1

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?

4
  • You are receiving integers as input. Are you expecting the user to enter either integer or quit ? Commented Mar 26, 2021 at 5:42
  • I edited your question so that it directly asks the question you are trying to ask and avoids off-topic chatter. Please pay attention to the differences, so that you can ask more effective Stack Overflow questions in the future. Commented Mar 26, 2021 at 5:45
  • Anyway, you should show us what you tried that didn't work, not just the part you have working so far. You tell us that you couldn't figure out how to use break, but presumably you tried to use break somehow/where. What exactly did you try? What happened when you tried that? If you got an error, you should try to understand the error, and also show the error message. You should also explain more precisely how you want it to work. For example, is the idea that the user will type quit *in response to the Please enter the announced Bingo Number: question? Or will you ask separately? Commented Mar 26, 2021 at 5:47
  • Thank you @KarlKnechtel, I really appreciate your edited version, it made everything simpler and a much better understanding of my needs. Without any doubts, I will follow your recommendations for my next question, exposing my mistakes and trying to explain better how I want it to work. Amazing! Thanks! Commented Mar 27, 2021 at 3:20

3 Answers 3

4

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!!!")
Sign up to request clarification or add additional context in comments.

7 Comments

you can remove the if statement and use while ... else The else will get executed only if the while statement is false. It will NOT execute if you break out of the while statement. To ensure you print Okay bye!, add the print statement before break
Please do not just give fully worked-out code without an explanation. You should talk about the things you needed to change, and why.
@JoeFerndz Great suggestion! I didn't know while ... else syntax existed. Thanks a lot---I'll edit the answer. @KarlKnechtel Good point indeed, I tried to submit the answer first and then edit the answer to give the explanation. But thanks for supporting productive environment.
since you are checking for lowercase quit, make sure you give user_input.lower() == 'quit'
Here's more information about while else...stackoverflow.com/questions/3295938/…
|
0

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

1 Comment

I really like the idea of making a list of 'quits' to give the user more option of typing, but after quit, the program still shows BINGO! in the end, this issue was also solved in the answer above. Thanks for your time and solution idea @Lostman
-1

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.

5 Comments

you can make while statement as while ch.lower() != 'q' or len(bingoCard) == 0. That way you dont need to have another if statement inside
Not the most efficient way to do this
@JoeFerndz in that case, the control would go inside the loop even if the length of the list is 0. You mean while ch.lower() != 'q' and len(bingoCard) != 0?
I have added another if inside just to ensure that the program does not ask the user if he wants to continue or quit in case he gets a BINGO in that iteration.
oops. it should have been and instead of or . The updated while statement should be while ch.lower() != 'q' or len(bingoCard) != 0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.