Python Forum

Full Version: Number range?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone, i am learning python and want to know if there is a way to display an error if someone goes past a specific value,
For example here i want to display an error if they go below 0 to negatives or anything above 1.0.

score = input("Enter Score between 0.0 and 1.0: ")
flscore = float (score)
????????????????????????????:
print(Error)

Plase note that i also want to display a message if they choose a number in between those numbers for example
if flscore >= 0.9:
print (msg)
dunce = True
while dunce:
    flscore = float(input('Enter a number between 0 and 1: '))
    if flscore<0 or flscore>1.0:
        print('Cant you read! Try that again and your computer will shock you!')
    else:
        dunce = False
print('Moving on')
if statement:
if flscore > 1:
    print("Error: your number is too high")
elif flscore < 0:
    print("Error: your number is too high")
else:
    print("your message")
Alright thank you all! so now the code is working but now i would like to know how to display another error if the users types a letter or anything else that is not a number.

score = input("Enter Score between 0.0 and 1.0: ")
flscore = float(score)
if flscore<0.0 or flscore>1.0:
print ("Error please stay in range of value")
elif flscore >= 0.9:
print("A")
elif flscore >= 0.8:
print ("B")
elif flscore >= 0.7:
print ("C")
elif flscore >= 0.6:
print("D")
elif flscore < 0.6:
print("F")
else:
print ("error 404")
For that you want to use a try...except and catch a Value Error.
dunce = True
while dunce:
    try :
        flscore = float(input('Enter a number between 0 and 1: '))
    except ValueError:
        print('Invalid entry')
    else :
        if flscore<0 or flscore>1.0:
            print('Cant you read! Try that again and your computer will shock you!')
        else:
            dunce = False
print('Moving on')
(Feb-18-2020, 12:16 PM)jefsummers Wrote: [ -> ]For that you want to use a try...except and catch a Value Error.
dunce = True
while dunce:
    try :
        flscore = float(input('Enter a number between 0 and 1: '))
    except ValueError:
        print('Invalid entry')
    else :
        if flscore<0 or flscore>1.0:
            print('Cant you read! Try that again and your computer will shock you!')
        else:
            dunce = False
print('Moving on')

Thank you very very much!