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?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
ejanasi
  • 13
  • 5
  • You are receiving integers as input. Are you expecting the user to enter either integer or `quit` ? – Joe Ferndz Mar 26 '21 at 05: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. – Karl Knechtel Mar 26 '21 at 05: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? – Karl Knechtel Mar 26 '21 at 05: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! – ejanasi Mar 27 '21 at 03:20

3 Answers3

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!!!")
j1-lee
  • 13,764
  • 3
  • 14
  • 26
  • 1
    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 – Joe Ferndz Mar 26 '21 at 05:48
  • 2
    Please do not just give fully worked-out code without an explanation. You should talk about the things you needed to change, and why. – Karl Knechtel Mar 26 '21 at 05:48
  • @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. – j1-lee Mar 26 '21 at 05:54
  • 1
    since you are checking for lowercase `quit`, make sure you give `user_input.lower() == 'quit'` – Joe Ferndz Mar 26 '21 at 05:55
  • Here's more information about while else...https://stackoverflow.com/questions/3295938/else-clause-on-python-while-statement – Joe Ferndz Mar 26 '21 at 05:58
  • Uau! Separating the user_input and the nNumberCalled in two different variables made everything make much more sense to me now. I didn't know either about the while ... else syntax. Have learned heaps with you guys. Cheers @j1-lee – ejanasi Mar 29 '21 at 09:58
  • My reply goes to you as well @JoeFerndz. Much appreciate. – ejanasi Mar 29 '21 at 09:59
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!!!")
BetterCallMe
  • 636
  • 7
  • 15
  • 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 – ejanasi Mar 29 '21 at 10:06
-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.

Suyash Krishna
  • 241
  • 2
  • 8
  • 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 – Joe Ferndz Mar 26 '21 at 05:50
  • 1
    Not the most efficient way to do this – Siddharth Agrawal Mar 26 '21 at 05:51
  • @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````? – Suyash Krishna Mar 26 '21 at 06:14
  • 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. – Suyash Krishna Mar 26 '21 at 06:18
  • oops. it should have been `and` instead of `or` . The updated while statement should be `while ch.lower() != 'q' or len(bingoCard) != 0` – Joe Ferndz Mar 26 '21 at 06:28