0

Every time I use the if statement directly after using input I get an error. For example:

num = input("Enter a number: ")
if num % 2 == 0:
  print("Even Number")
else:
  print("Odd Number")

I get this error:

Traceback (most recent call last): File "python", line 2, in TypeError: not all arguments converted during string formatting

What am I doing wrong?

Andrew Lastrapes
  • 187
  • 3
  • 15
  • `input` returns a string in Python 3 and Python thinks you're try to format with `%` here. You should convert to a numeric type – Moses Koledoye Mar 15 '17 at 17:03

2 Answers2

4

You are using Python 3, where input returns a string. Convert your input to an integer with int(input("Enter a number: ")).

timgeb
  • 76,762
  • 20
  • 123
  • 145
2

Input returns a string. You must typecast to an int.

num = int(input("Enter a number: " )

or

if int(num) % 2 == 0:

Eugene K
  • 3,381
  • 2
  • 23
  • 36