MrKeeplearning
MrKeeplearning

Reputation: 9

How can I get a result of a function in python which has input from user?

I was trying to make a function which name is is_odd and this function checks whether the number entered by the user is even, odd, or zero. I want to print the result of the function but it keeps making errors. Is there any other best way to print the result without using print(is_odd(num))?

This is my Code.

num = input()
def is_odd(num):
    if num == 0:
        return "zero"
    elif num % 2 == 0:
        return "even"
    else:
        return "odd"

print(is_odd(num))

And this is the error from the code.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-76-05eb9348733b> in <module>
      8         return "odd"
      9 
---> 10 print(is_odd(num))

<ipython-input-76-05eb9348733b> in is_odd(num)
      3     if num == 0:
      4         return "zero"
----> 5     elif num % 2 == 0:
      6         return "even"
      7     else:

TypeError: not all arguments converted during string formatting

Upvotes: 0

Views: 205

Answers (2)

Janska
Janska

Reputation: 41

The only thing you should change is your first line:

num = int(input())

to make sure that the input is an integer

Upvotes: 0

Ignacio Alorre
Ignacio Alorre

Reputation: 7605

You need to cast the input. By default what is captured by input() comes as a string, you need to transform it into an int in your case, since later you are comparing that value with integers

num = int(input())

Upvotes: 1

Related Questions