2 odpowiedzi
+ 3
Luke's answer, in short: quit()
+ 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!