How do yo make python exit if the input is not within range? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do yo make python exit if the input is not within range?

I have tried else: raise systemexit and exit() but they were not accptable by the the teacher any other simple solutions to making python stop to going to the next step if the input is not within range? This is a 1st level python class

30th Sep 2016, 5:03 AM
Steven Bagherloo
2 Answers
+ 3
Luke's answer, in short: quit()
30th Sep 2016, 3:57 PM
Kirk Schafer
Kirk Schafer - avatar
+ 1
while True: user = input('get input') if input not valid: print("invalid input") continue #restarts loop elif input == condition1: some code elif input == condition2: some code . . . Basically the first thing you have to do is evaluate the input to make sure it is valid. The First thing. If it is not valid, you can quit/exit, break the loop entirely, or ask for the input again. This is especially helpful if you write an app that will quit if the user ever enters the word 'quit' or has a 'help' command, or things like that. Consider the following calculator input print("Enter first number, or type 'quit' to quit") user = input(":"). lower() #makes string lowercase if user == "quit" : quit() elif user.isalpha(): #if input has letters print("error: letter entered") break else: Do the calculation. So the above checks for keywords entered first, then makes sure the input is valid, then executes code. Hope this helps!
30th Sep 2016, 3:08 PM
Luke Armstrong