I am currently reading "How to think like a computer scientist: Learning with Python" (Green Tea Press, January 2002.)
I can't make any define functions work. I have copied it exactly as it is in the book, but it is not working. What at I doing wrong?
Here is the code:
def printParity(x):
if (x)%2 == 0:
print (x), ("is even")
else:
print (x), ("is odd")
It just prints the (x) input rather that (x) is odd or even.
You want:
def printParity(x):
if x % 2 == 0:
print(x, "is even")
else:
print(x, "is odd")
Your statement print (x), ("is even")
is actually making a tuple, as can be seen at the console:
>>> x=2
>>> print (x), ("is even")
2
(None, 'is even')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With