简体   繁体   中英

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

I have this working code for a bingo-like game in Python (a winner is announced when the full card is matched):

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

while len(bingoCard) != 0:
    nNumberCalled = int(input("\nPlease enter the announced Bingo Number: "))
    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.")

print("\nBINGO!!!")

The idea is that I remove numbers from the bingoCard as they are called, until the list is empty.

I would like to give to the user the option to quit the game (break out of the loop) at any time by typing "quit".

I tried to research this question, but I couldn't figure out how or where to add a break statement into my code to make it work correctly. I guess I have to include something else such as try / except or maybe a for loop inside the while loop. How do I make this work?

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, ie, 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!!!")

First make a list of quits and put a break at proper place like this:

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

while len(bingoCard) != 0:
    new_var = input("\nPlease enter the announced Bingo Number: ")
    if(new_var in ['quit', 'Quit', 'QUIT']):
        break
    else:
        nNumberCalled = int(new_var)
        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.")

print("\nBINGO!!!")

Could give this a try:

bingoCard = [7, 26, 40, 58, 73, 14, 22, 34, 55, 68]
ch=''
while ch!='q':
    nNumberCalled = int(input("Please enter the announced Bingo Number: "))
    if nNumberCalled <1 or nNumberCalled > 80:
        print("Oops, the number should be between 1 and 80.")
    elif nNumberCalled in bingoCard:
        bingoCard.remove(nNumberCalled)
        print("Nice on1e! You hit ",nNumberCalled)
    else:
        print("Nah... Not in your card.")
    if len(bingoCard) == 0:
        break
    ch = input("Press q to quit or any other key to continue: ")

if(ch=='q'):
    print("Thank You")
else:
    print("\nBINGO!!!")

What I am doing is, keeping a variable to get the choice of the user in every iteration. If the user enters 'q', the code breaks out of the loop and checks for the last user input, else the game continues. Outside the loop, if it is found that the last user choice was 'q', it means that the user has quit the game, otherwise it prints BINGO. Please note, the other way of breaking out of the loop is when you have guessed all the numbers on your card.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM