 # Using Modulo with variables

Hi All,

Maybe a stupid question but I have been working with these https://www.codecademy.com/courses/learn-python-3/articles/python-code-challenges-functions

Quiz 5 asks: Write a function named `remainder()` that has two parameters named `num1` and `num2` . The function should return the remainder of twice `num1` divided by half of `num2` .

I got the solution as:

``````def remainder(num1, num2):
magic_number = (num1 * 2) % (num2 / 2)
return magic_number

print(remainder(15,14))
print(remainder(9,6))
``````

Which is correct.
I first tried the following, Ive also searched all over for the answer:

Is it possible to do this:

``````def remainder(num1, num2):
magic_number = (num1 * 2) % (num2 / 2)
return magic_number

#calculate num1
num1_twice = num1 * 2
return num1_twice

#calculate num2
num2_half = num2 / 2
return num2_half

#calculate remainder with Modulo between the two variables
remainder_mod = num1_twice % num2_half
return remainder_mod

print(remainder(15,14))
print(remainder(9,6))
``````

Can Modulo be used like this?

Thanks,

Hi there.

The code in your second example, where you’re calculating `num1_twice` and `num2_half` has a couple of mistakes in it.

That aside, the crux of your question is whether the following is valid:

``````num1 = 15
num2 = 14

num1_twice = num1 * 2
num2_half = num2 / 2

remainder_mod = num1_twice % num2_half
``````

``````print(remainder_mod) #output: 2
``````

The exercise requires that we don’t define a fixed number for num 1 and num 2 and only define it at the end within the print statement.

How will this affect it ?

As in

``````def remainder(a,b):
return a % b

print(remainder(30,7))
``````

?

Works the same. So that’s what I tried. I needed to first calculate what double of num1 would be and what half of num2 would be. Once I had those I tried num1 % num2 which didn’t work

Would that have been this attempt?

``````def remainder(num1, num2):
magic_number = (num1 * 2) % (num2 / 2)
return magic_number

#calculate num1
num1_twice = num1 * 2
return num1_twice

#calculate num2
num2_half = num2 / 2
return num2_half

#calculate remainder with Modulo between the two variables
remainder_mod = num1_twice % num2_half
return remainder_mod
``````

The above code won’t work, but not because of the `%` operator…

Yes, Ok, so what is the reason?
But I am also asking generally if the % can be used like that?

You need to re-visit the role that the `return` keyword serves, and what its purpose is… Any errors you’re getting from the code in my previous post will stem from that, and an indentation error with your defined `remainder()` function.