view raw
meatduck12 meatduck12 - 6 months ago 27
Python Question

Having trouble getting my else statement to print in a while loop

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:
elif OVR < 50:
elif OVR >= 50 and OVR < 60:
elif OVR >= 60 and OVR <= 70:
elif OVR > 70 and OVR <= 82:
elif OVR > 82:
print("He's the GOAT, what do you think he wants?")
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.


Wrap the int conversion in a try/except block.

while True:
        OVR = int(input('OVR?'))
    except ValueError:
        print('different values needed')
    # OVR an integer value, handle it as needed...