+ 2

Complex odd vs even coding

Trying to make a complicated odd vs even code, and I think I have it close to correct, but not sure what I'm doing wrong: def a_func(x): num = int(input("Enter a number:") remainder = num % 2 if remainder = 0: return ("Even so, yes.") else: return ("That's quite an odd number.") print(a_func) I'm getting this error: File "..\Playground\", line 3 remainder = num % 2 ^ SyntaxError: invalid syntax Any ideas why? And if so, what can I do to fix it?

24th Jan 2018, 3:56 AM
ParadoxRegiment
ParadoxRegiment - avatar
5 odpowiedzi
+ 4
I'm pretty sure the reason for that is because you forgot to add the double equal sign for the if statement, which makes it conditional rather than making it for declaring variables. Try changing it to this: if remainder == 0: #your code goes here
24th Jan 2018, 4:00 AM
Faisal
Faisal - avatar
+ 11
Hi @ParadoxRegiment @Faisal is right - the single = sign is for assigning a value, the double == is the test for equality. Other things that will give you errors: Don't put x in def a_func(x) if you aren't going to apply the function to a parameter x. Just use def a_func() When you call the function, include the parentheses, i.e. print(a_func()) not print(a_func) you forgot the last ) in the num = etc. line Here it is with the fixes 🙂 def a_func(): num = int(input("Enter a number:")) remainder = num % 2 if remainder == 0: return ("Even so, yes.") else: return ("That's quite an odd number.") print(a_func()) p.s. you can simplify the code by just saying: if num % 2 == 0: edit. Or even: if int(input("Enter a number:")) % 2 == 0: 😉
24th Jan 2018, 5:17 AM
David Ashton
David Ashton - avatar
+ 2
Alright, I'll try that. Thanks!
24th Jan 2018, 4:02 AM
ParadoxRegiment
ParadoxRegiment - avatar
+ 2
Ohh, ok. That makes sense. Thanks for responding!
24th Jan 2018, 2:32 PM
ParadoxRegiment
ParadoxRegiment - avatar
+ 2
Fixed it, and it works! Thanks guys, here's the code if you wanna mess with it. https://code.sololearn.com/c6N6mMRV37F3/?ref=app
24th Jan 2018, 2:38 PM
ParadoxRegiment
ParadoxRegiment - avatar