Python Forum

Full Version: TypeError: not all arguments converted during string formatting
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Why is a basic print statement asking for string formatting?:

#!/usr/bin/env python3 #PracticePythonExercise02.py number = input("Enter a number: ") if number % 2 == 0: print("the number is even.") else: print("the number is odd.")
Error:
>>> ================================ RESTART ================================ >>> Enter a number: 4 Traceback (most recent call last): File "F:/Python/Python36-32/SamsPrograms/PracticePythonExercise02.py", line 6, in <module> if number % 2 == 0: TypeError: not all arguments converted during string formatting >>>
It's not. Note that the line referenced in the traceback is line 6, the conditional, not the print statement. In Python 3.x, input returns a string. With strings, % is the (old) string formatting operator. You need to convert number to a number with int() before the conditional. Then % will be the mod operator, and will give you what you want.