How would I type a string in an int input without getting an Error in Python?
I was wondering how I would allow my input to allow both strings and int. For example, if I wanted to type 'string' I wouldn't get an error. But when I type a string into it I get this error: ValueError: invalid literal for int() with base 10: 'string' I know I have an int() at the start of the input line but I've tried other things and it still doesn't work. This is the code:
row = int(input('Which row do you want to change?: '))
Solution 1:
You can validate the input rather than arbitrarily converting it to integer.
row = input('Which row do you want to change?: ')
if row.isdigit():
row = int(row)
else:
print("That's a string")
Solution 2:
while True:
try:
age = int(input("Which row do you want to change? "))
except ValueError:
print("Sorry, I didn't understand that.")
continue
else:
break