0

I'm really new to python, and I'm having trouble with the code

def even(num):
    if num % 2 == 0:
        return "Even"
    else:
        return "Odd"

one = input("Number: ")
print(even(one))

I want it to ask the user for a number, then print if it's even or odd, but every time I execute the program and type in a number it gives me an error. Any ideas on what I'm missing or doing wrong? Much appreciated. Thanks.

gyre
  • 16,369
  • 3
  • 37
  • 47
RugbugRedfern
  • 43
  • 1
  • 9

2 Answers2

2

input returns str object and you are trying to act with it like it has type int, just cast user input

one = int(input("Number: "))
Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
1

Your code run without problem in python2.x. In python3.x, you need to update your code to this

def even(num):
    if num % 2 == 0:
        return "Even"
    else:
        return "Odd"

one = int(input("Number: ")) # here is the update
print(even(one))

related questions on intput

Community
  • 1
  • 1
go4big
  • 21
  • 4
  • It should be mentioned, that `input` on Python 2 will lead to severe security problems. So the code for Python 2 should look like: `one = int(raw_input("Number: "))` – Matthias Apr 26 '17 at 07:19