Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Number range?
#1
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)
Reply
#2
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')
Reply
#3
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")
Reply
#4
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")
Reply
#5
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')
Reply
#6
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  matplotlib x axis range goes over the set range Pedroski55 5 3,104 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 6,877 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  Is 2 a prime number? for loop & range fuction in python docs says yes, mine says no. allusernametaken 4 2,844 Nov-17-2019, 02:56 AM
Last Post: allusernametaken
  Number within range MrGoat 4 3,175 Jan-27-2019, 05:51 PM
Last Post: aakashjha001

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020