Reputation: 23
Still very new to Python, so I apologize if anything in here is off. This is for a program where you enter a number, and get back a value. You have to enter a number, so I'm trying to make it say "different values needed" when the user enters a string. However, I have an int() around the input, which means I get the following error when I input a string:
ValueError: invalid literal for int() with base 10
My code is as follows:
while True:
OVR = int(input('OVR?'))
if OVR == 0:
break
elif OVR < 50:
print('0.75M')
elif OVR >= 50 and OVR < 60:
print('8M')
elif OVR >= 60 and OVR <= 70:
print('15M')
elif OVR > 70 and OVR <= 82:
print('30M')
elif OVR > 82:
print("He's the GOAT, what do you think he wants?")
else:
print('different values needed')
I know it's probably a bad idea to have all those elifs, so I will eventually consolidate that into one formula. As said earlier, my main goal is that I want to make this so that it prints "different values needed" when the user enters a string. I've considered a try/except statement, but if I'm understanding them correctly, they are not really for calculating and printing things, and also couldn't take these elifs.
Upvotes: 2
Views: 103
Reputation: 929
You can use a try/except statement to print a message.
try:
OVR = int(input("OVR?"))
except ValueError:
print("different values needed")
Upvotes: 0
Reputation: 15240
Wrap the int
conversion in a try
/except
block.
while True:
try:
OVR = int(input('OVR?'))
except ValueError:
print('different values needed')
continue
# OVR an integer value, handle it as needed...
Upvotes: 1