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
8 Réponses
+ 2
"if num1 %=0" is not a correct statement.
i am not a pro in python but try if num1=0 or num1=0%
+ 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))
+ 1
there is no meaning to num1 %=0
+ 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
+ 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))
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
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.
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))