Python Forum
TypeError: not all arguments converted during string formatting
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: not all arguments converted during string formatting
#1
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 >>>
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: string indices must be integers deneme2 2 604 Feb-14-2025, 12:23 AM
Last Post: deneme2
  TypeError: string indices must be integers, not 'str' LEMA 2 2,263 Jun-12-2024, 09:32 PM
Last Post: LEMA
  Formatting DateTime string and and converting it from AM/PM to 24 hours tester_V 2 1,047 Jun-08-2024, 05:16 PM
Last Post: tester_V
  Formatting a date time string read from a csv file DosAtPython 5 4,859 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  boto3 - Error - TypeError: string indices must be integers kpatil 7 3,048 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Converted EXE file size is too large Rajasekaran 0 2,576 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 5,641 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  String formatting (strptime) issues Henrio 2 1,776 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 3,401 Oct-17-2022, 06:29 PM
Last Post: paulo79
  TypeError: string indices must be integers JonWayn 12 5,740 Aug-31-2022, 03:29 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020