How to End Program If Input == "Quit" With Many If Statements

How to end program if input == "quit" with many if statements?

Create a function, use it each time you taker an input, call "exit()" to leave

For example

import sys

def check_quit(inp):
if inp == 'quit':
sys.exit(0)

Is there a way to stop a program from running if an input is incorrect? Python

First import these :-

import sys

Then add this after that "sorry we don't allow under 18" line

input('Press Enter to Continue...')
sys.exit()

Plus use if instead of while

Your code can be simplified as :-

import sys

first_name=input('PLEASE ENTER YOUR FIRST NAME: ')
last_name=input('PLEASE ENTER YOUR SURNAME: ')
print(f'Hello {first_name} {last_name}, before you enter.')

def age_det():
age = int(input(f'How old are you?'))
if num>=18:
print(f'Awesome, {first_name}, welcome to Blablabla')
else :
print(f'Sorry, {first_name}, but we require you to be at least 18 to enter Blablabla.')
input('Press Enter to Continue...')
sys.exit()

age_det()

How do I stop my program from jumping to a different if-Statement after receiving an invalid input?

So, packaging your choices into a dictionary, similar to that shown below, should make it slightly easier to manage the choices here, I think (there's almost certainly a better way than this). Then add to the empty string each time a choice is made and try to access the dictionary. If the choice is in the dictionary then it will recover a text string and an end-state, which will enable us to end the game when we need to.

This approach also makes testing easier by using itertools to generate all possible combinations of states so you can work out which are missing. If an end_state is found (a value of 1 in the second position of the tuple), then you get the game over message and it closes the loop. If the element isn't in the dictionary, then the last selection was removed and the invalid_input function is called.

def test():

choice_dict = {"a": (dP_lvl1.path_a, 0),
"b": (dP_lvl1.path_b, 0),
"c": (dP_lvl1.path_c, 1)
"bb": (dP_lvl2.path_bb, 0),
"aa": (dP_lvl2.path_aa, 0),
"ba": (dP_lvl2.path_ba, 0),
"ab": (dP_lvl2.path_ab, 0),
"aaa": (dP_lvl3.path_aaa, 0),
"aab": (dP_lvl3.path_aab 0),
"aba": (dP_lvl3.path_aba, 0),
"abb": (dP_lvl3.path_abb, 0),
"bab": (dP_lvl3.path_bab, 0),
"bba": (dP_lvl3.path_bba} 0),
"bbb": (dP_lvl3.path_bbb, 0),
"aaaa": (dP_lvl4.path_aaaa, 0),
"abaa": (dP_lvl4.path_abaa, 0),
"aaba": (dP_lvl4.path_aaba, 0),
"aaab": (dP_lvl4.path_aaab, 1),
"bbba": (dP_lvl4.path_bbba, 0),
"bbab": (dP_lvl4.path_bbab, 0),
"babb": (dP_lvl4.path_babb, 0),
"abbb": (dP_lvl4.path_abbb, 0),
"abba": (dP_lvl4.path_abba, 1),
"abab": (dP_lvl4.path_abab, 0),
"aabb": (dP_lvl4.path_aabb, 0),
"baab": (dP_lvl4.path_baab, 0),
"bbaa": (dP_lvl4.path_bbaa, 1),
"baba": (dP_lvl4.path_baba, 0),
"baaa": (dP_lvl4.path_baaa, 0),
"bbbb": (dP_lvl4.path_bbbb, 0),}
# etc. you get the idea

decisions = ""
playing = True
while playing:
decision = input("choose an option 'a' or 'b':")
decisions += decision

try:
data, end_state = choice_dict[decisions]
print(data)
if end_state:
playing = False
print("Game over")
else:
continue
except KeyError:
decisions = decisions[:-1]
invalid_input()

User input to stop an if statement within a for loop

As you might have guessed, the problem is in your if condition. Change it to

if change_choice == 'y' or change_choice == 'Y':

and

elif change_choice == 'n' or change_choice == 'N':

and it should work fine.

If you don't want to have to repeat the comparison with ==, you can instead check whether your input variable's value is in a sequence type (tuple or list, for example) containing all the strings you wish to include. This comparison:

if change_choice in ('y', 'Y')

is equivalent to the above.

Break a loop and quit the program when user types ''quit'' in Python

How about receiving the input, and then breaking out from the while loop if the string is quit? If the input is not quit, then proceed as you did, i.e., parse the input as integer.

Also note that you wouldn't want to just invoke break, because that would let the user see "BINGO" message even if he/she quits. To address this issue, as per a suggestion by @JoeFerndz, while ... else clause is used. This clause is what I was not aware of, and I think it is very useful. Thank you for the question (and of course @JoeFerndz as well for the comment), from which I could learn something new!

bingoCard = [7, 26, 40, 58, 73, 14, 22, 34, 55, 68]

while len(bingoCard) != 0:
user_input = input("\nPlease enter the announced Bingo Number (or 'quit'): ")
if user_input.lower() == 'quit':
print("Okay bye!")
break
nNumberCalled = int(user_input)
if nNumberCalled <1 or nNumberCalled > 80:
print("Oops, the number should be between 1 and 80.")
elif nNumberCalled in bingoCard:
bingoCard.remove(nNumberCalled)
print(f"Nice on1e! You hit {nNumberCalled}.")
else:
print("Nah... Not in your card.")
else:
print("\nBINGO!!!")

Is there a way to end the program if statement is false?

if (age > 13) {
System.out.println("You are eligible for this site, you may proceed. ");
}else{
System.exit(0);
}

This should end it!



Related Topics



Leave a reply



Submit