while True:
choice = input("WELCOME TO ASSOCIATION SCRIPT #0001. TYPE BEGIN TO START THIS SCRIPT. MISENTRY OF ANY FIELD WILL REQUIRE A RESTART OF THIS PROGRAM. ENSURE ALL INPUTTED TEXT IS IN UPPERCASE. = ")
#Essentially the user types "BEGIN" for the login process to begin.
if choice == "BEGIN":
print("PLEASE ENTER YOUR USERNAME AND BADGE NUMBER.")
break
else:
print("INVALID INPUT, PLEASE RELOAD THE TERMINAL")
break
Is there a way to stop all future lines of code from running after the else statement here?
I haven't really done much because I didn't know where to start. I'm also VERY new to Python.
CodePudding user response:
an option is to put the block into a function and return the function
CodePudding user response:
There are two ways of doing that. First way is to use exit or sys.exit as the other answer suggests.
But the other way which I happen to prefer is to wrap your code inside a main()
function instead of the global area (as shown below). That way, you can exit the program using a simple return
statement which will be consistent with what you'll be doing everywhere else:
def main():
# start your code here
# if not foo: return
pass
if __name__ == "__main__":
main()
The main()
function here will also be consistent with how programs in most other languages approach initialization of routines (like void main()
in java, for example).
CodePudding user response:
I understand that you want to close the python application completely if the login is not success. you may stop python execution programmatically with the help of built in function exit ()
. Here is the code I modified. I don't think while loop is required here.
choice = input("WELCOME TO ASSOCIATION SCRIPT #0001. TYPE BEGIN TO START THIS SCRIPT. MISENTRY OF ANY FIELD WILL REQUIRE A RESTART OF THIS PROGRAM. ENSURE ALL INPUTTED TEXT IS IN UPPERCASE. = ")
#Essentially the user types "BEGIN" for the login process to begin.
if choice == "BEGIN":
print("PLEASE ENTER YOUR USERNAME AND BADGE NUMBER.")
else:
print("INVALID INPUT, PLEASE RELOAD THE TERMINAL")
exit()
details = input("---your other code here---")