Skip to content

Latest commit

 

History

History
39 lines (27 loc) · 714 Bytes

File metadata and controls

39 lines (27 loc) · 714 Bytes

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

Occurs when trying to convert an empty string to an integer.

Reproduce

age = input("Enter your age: ")

number = int(age)
print(number)

Error Message

Enter your age: 
Traceback (most recent call last):
  File "reproduce.py", line 3, in <module>
    number = int(age)
ValueError: invalid literal for int() with base 10: ''

Fix

age = input("Enter your age: ")

if age.strip() == "":
    print("Please enter a number")
else:
    number = int(age)
    print(number)

Reflection

I just pressed enter without typing anything. input() returns an empty string in that case, which cannot be converted to int.