Number range? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Number range? (/thread-24521.html) |
Number range? - rusty11 - Feb-18-2020 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) RE: Number range? - jefsummers - Feb-18-2020 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') RE: Number range? - michael1789 - Feb-18-2020 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") RE: Number range? - rusty11 - Feb-18-2020 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") RE: Number range? - jefsummers - Feb-18-2020 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') RE: Number range? - rusty11 - Feb-18-2020 (Feb-18-2020, 12:16 PM)jefsummers Wrote: For that you want to use a try...except and catch a Value Error. Thank you very very much! |