← All Python errors

Python error explained

ValueError: what it means and how to fix it

The type is right but the value inside it is not.

The message

ValueError: invalid literal for int() with base 10: 'twelve'

What it means

int() can turn the text "12" into the number 12, but it cannot make a number out of "twelve" or "12.5". The value has the right type (text) but the wrong content.

Common causes

  • Converting text that contains letters or spaces
  • Using int() on a decimal like "3.5" (use float())
  • Empty input when the user just pressed Enter

Example that raises it

answers = ["12", "7", "twelve", "3"]
total = 0
for a in answers:
    total += int(a)
print("Total:", total)

The fix

answers = ["12", "7", "twelve", "3"]
total = 0
for a in answers:
    if a.isdigit():
        total += int(a)
    else:
        print("Skipping", a)
print("Total:", total)

Tip: Check first with .isdigit(), or wrap the conversion in try and except ValueError to handle bad input gracefully.

Fix the broken version yourself in the editor on this page. It runs real Python in your browser.

Start the free lessons More Python errors