Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User defined function (def) in Python 3.2.2

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.

like image 694
user1636588 Avatar asked Jan 16 '23 16:01

user1636588


1 Answers

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')
like image 161
DSM Avatar answered Jan 18 '23 23:01

DSM