ValueError when parsing input in Python (Fix)

ValueError often happens when your program reads text with input() and then tries to convert that text into a number.

This usually affects code that uses int() or float(). For example, the user might type letters instead of digits, leave the input blank, or enter a decimal when your code expects a whole number.

This page shows:

  • what the error means
  • how to reproduce it
  • simple ways to fix it
  • how to prevent it in beginner-friendly code

Quick fix #

user_text = input("Enter a number: ").strip()

try:
    number = int(user_text)
    print("You entered:", number)
except ValueError:
    print("Please enter a whole number only.")

Use try-except when converting input with int() or float(). strip() also helps remove accidental spaces.

What this error means #

ValueError means Python received a value of the correct general type, but the content is not valid for the operation.

When parsing input, this often happens when you try to:

  • convert text to an integer with int()
  • convert text to a decimal number with float()
  • unpack values from split() and the number of parts is wrong

Common examples:

  • int("abc")
  • int("")
  • float("")
  • a, b = input().split() when the input does not contain exactly two parts

If you are new to exceptions, see Python errors and exceptions explained.

Common example that causes the error #

A very common beginner pattern is this:

age = int(input("Enter your age: "))
print("Next year you will be", age + 1)

This works only if the user enters a valid whole number.

If the user types hello, Python raises an error:

age = int(input("Enter your age: "))

Example input:

hello

Typical error message:

ValueError: invalid literal for int() with base 10: 'hello'
Traceback (most recent call last):File "example.py", line 1, in <module>age = int(input("Enter your age: "))ValueError: invalid literal for int() with base 10: 'hello'Where it happened — file and lineWhat went wrong — the exception typeWhy — the detailed message
Read it bottom-up: input() returned the string ‘hello’, which int() cannot convert.

That happens because input() always returns a string. If you want to learn more about that, see Python input() function explained and Python int() function explained.

Fix 1: Use try-except around the conversion #

This is the best fix when input comes from a real user.

Wrap the conversion in a try-except block so your program does not crash.

user_text = input("Enter a whole number: ")

try:
    number = int(user_text)
    print("Valid number:", number)
except ValueError:
    print("That was not a valid whole number.")

What this does:

  • try: runs the risky code
  • if int(user_text) fails, Python jumps to except ValueError:
  • your program prints a helpful message instead of stopping

You can do the same with float():

user_text = input("Enter a decimal number: ")

try:
    number = float(user_text)
    print("Valid number:", number)
except ValueError:
    print("Please enter a valid number.")

If you want more practice with this pattern, see how to use try-except blocks in Python.

Fix 2: Validate input before converting #

Sometimes you want to reject bad input before calling int().

For simple positive integers, isdigit() can help:

user_text = input("Enter a whole number: ").strip()

if user_text.isdigit():
    number = int(user_text)
    print("Valid number:", number)
else:
    print("Please enter digits only.")

This works for values like:

  • "5"
  • "42"
  • "123"

But it does not work for:

  • negative numbers like "-5"
  • decimals like "3.14"
  • empty strings like ""

So isdigit() is useful for simple checks, but it is not a complete replacement for try-except.

Fix 3: Clean the input first #

Sometimes the text is almost correct, but it contains spaces or formatting that causes problems.

Use strip() to remove spaces at the start and end:

user_text = input("Enter a number: ").strip()

try:
    number = int(user_text)
    print(number)
except ValueError:
    print("Invalid input")

This helps with input like:

"  25  "

You can also clean other formatting if needed. For example, some users may paste numbers with commas:

user_text = input("Enter a number: ").strip()
user_text = user_text.replace(",", "")

try:
    number = int(user_text)
    print("Parsed number:", number)
except ValueError:
    print("Please enter a valid whole number.")

Example:

  • "1,000" becomes "1000"

Be careful with replacements. Only clean the formats you actually expect.

Fix 4: Ask again until the input is valid #

For many beginner programs, the best user experience is to keep asking until the user enters valid input.

while True:
    user_text = input("Enter a whole number: ").strip()

    try:
        number = int(user_text)
        break
    except ValueError:
        print("That was not a valid whole number. Try again.")

print("You entered:", number)

Why this is useful:

  • the program does not crash
  • the user gets another chance
  • your code stays simple and focused

You can use the same pattern with float() if you expect decimal input. For a full walkthrough, see how to convert user input to numbers in Python.

Special cases beginners hit #

Some ValueError cases are confusing at first. Here are a few common ones.

int('3.5') #

print(int("3.5"))

This raises ValueError because "3.5" is a decimal string, not a whole number string.

If you expect decimal input, use float() instead:

print(float("3.5"))

float('') #

print(float(""))

This raises ValueError because an empty string is not a number.

This often happens when:

  • the user just presses Enter
  • you forgot to check for empty input

Unpacking from split() #

a, b = input("Enter two values: ").split()
print(a, b)

This works only if the input has exactly two parts.

For example:

  • input: "10 20" → works
  • input: "10" → not enough values
  • input: "10 20 30" → too many values

These cases can raise different ValueError messages. See:

How to debug the input value #

If the error does not make sense, inspect the actual input.

Useful debugging lines:

user_input = input("Enter something: ")

print(user_input)
print(repr(user_input))
print(type(user_input))

Why repr() helps:

  • it shows hidden spaces
  • it makes empty strings easier to see
  • it can reveal unexpected characters

Example:

user_input = " 42 "
print(user_input)        # looks like:  42
print(repr(user_input))  # shows: ' 42 '

For split() problems, inspect the list of parts:

parts = input("Enter values: ").split()
print(parts, len(parts))

This helps you see whether you got the number of values you expected.

When to use int() vs float() #

Choose the conversion based on the kind of number you expect.

Use int() for whole numbers:

  • 5
  • 42
  • 100

Use float() for decimal numbers:

  • 3.14
  • 2.0
  • 0.5

Example:

whole = int("5")
decimal = float("3.14")

print(whole)
print(decimal)

If your program may receive either whole numbers or decimals, float() is often the better choice.

For example:

user_text = input("Enter a number: ").strip()

try:
    number = float(user_text)
    print("Valid number:", number)
except ValueError:
    print("Please enter a valid number.")

If needed, you can later convert a valid float to an integer, but only if that makes sense for your program. See Python float() function explained for more details.

Common causes #

This error often happens because:

  • user entered letters instead of digits
  • user entered a decimal when int() was used
  • input was empty
  • input had extra spaces or formatting
  • code unpacked the wrong number of values from split()
  • program assumed input was always valid

FAQ #

Why does int('3.0') raise ValueError? #

Because int() expects a whole number string like "3", not a decimal string like "3.0". Convert with float() first if needed.

Does strip() fix ValueError by itself? #

Only sometimes. It removes spaces at the start and end, but it does not turn invalid text like "abc" into a number.

Should I use isdigit() instead of try-except? #

isdigit() is useful for simple cases, but try-except is more flexible and handles more real input situations.

Can input() return an int automatically? #

No. input() always returns a string, so you must convert it yourself.

See also #

Press Esc to close