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
  Formatting a date time string read from a csv file DosAtPython 5 1,253 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,232 Jun-09-2023, 06:56 PM
Last Post: kpatil
  Converted EXE file size is too large Rajasekaran 0 1,509 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,013 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  String formatting (strptime) issues Henrio 2 839 Jan-06-2023, 06:57 PM
Last Post: deanhystad
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,935 Oct-17-2022, 06:29 PM
Last Post: paulo79
  TypeError: string indices must be integers JonWayn 12 3,383 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  Python converted EXE not working Rajasekaran 1 1,578 Aug-22-2022, 05:56 PM
Last Post: hismail_sa
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,852 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Passing string functions as arguments Clunk_Head 3 1,244 Jun-15-2022, 06:00 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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