0

What's wrong here?

d = 365 w = 7 def divi(x,y): num1 = (x // y) num2 = (x % y) if num1 %=0: return str(num1) else: return str(num1 + "r" + num2) print(divi(d,w)) This always produces an error at num1 on line 7,however I thought this is was correct formatting. I'm sure it's something silly I've overlooked

31st Oct 2016, 11:31 AM
Jethro Bridge
Jethro Bridge - avatar
8 odpowiedzi
+ 2
"if num1 %=0" is not a correct statement. i am not a pro in python but try if num1=0 or num1=0%
31st Oct 2016, 11:41 AM
Laurens Verbruggen
Laurens Verbruggen - avatar
+ 2
firstly..the variables x and y weren't defined secondly.. it should be num1==0 thirdly..a number and a string cannot be added ..so it has to be : return str(str(num1) + "r" + str(num2))
31st Oct 2016, 11:46 AM
Tirthashree
Tirthashree - avatar
+ 1
there is no meaning to num1 %=0
31st Oct 2016, 11:39 AM
Tirthashree
Tirthashree - avatar
+ 1
if you were trying to get the code to return to num1 if there is no remainder..it has to be num2 == 0 and not num1 == 0 because num2 is the remainder and num1 is the quotient
31st Oct 2016, 11:53 AM
Tirthashree
Tirthashree - avatar
+ 1
Thanks tirtha, I had a bit of a logic problem :) d = 365 w = 7 def divi(x,y): num1 = (x // y) num2 = (x % y) if num2 ==0: return str(num1) else: return str(str(num1) + "r" + str(num2)) print(divi(d,w))
31st Oct 2016, 11:59 AM
Jethro Bridge
Jethro Bridge - avatar
0
OK, I was trying to get the code to return num1 if there is no remainder, I thought %= could be used like == I'll have to rethink. Thanks
31st Oct 2016, 11:47 AM
Jethro Bridge
Jethro Bridge - avatar
0
Actually, that might make sense, if it was not 0. That num1 %= 0 is like division with zero, which produces error. But I am not sure.
31st Oct 2016, 11:48 AM
Daniel Oravec
Daniel Oravec - avatar
0
Thanks for all your fast help. I've got it working, could maybe be for elegant but I can't see it right now d = 365 w = 7 def divi(x,y): num1 = (x // y) num2 = (x % y) if (num1/num1) ==0: return str(num1) else: return str(str(num1) + "r" + str(num2)) print(divi(d,w))
31st Oct 2016, 11:56 AM
Jethro Bridge
Jethro Bridge - avatar