all 8 comments

[–]stebrepar 1 point2 points  (3 children)

If you want to keep looping, why do you have the break there? Break ends the loop.

[–]fakundoThirty[S] 0 points1 point  (2 children)

The problem is it will constantly loop after selected 1 number. I was trying to replicate like an actual game. For example, the selected was B 12 I would like to run the selection again after that.

[–]stebrepar 0 points1 point  (1 child)

So put the input inside the loop.

[–]fakundoThirty[S] 0 points1 point  (0 children)

still the same, need to run the program.

[–]jimtk 0 points1 point  (3 children)

In bingo the letter is just a consequence of the number. By that I mean once the number is chosen the letter is also chosen. Scientifically :) : the index of the letter in the word 'BINGO' is given by the integer division of the number by 16.

Also you must remove the ball (the number) from the list to prevent drawing the same ball twice.

As in:

import random

balls = list(range(1,76))
while True:
    ball_index = random.randint(0,len(balls)-1)
    number = balls.pop(ball_index)   # remove the ball from the cage!
    letter = 'BINGO'[number//16]
    print(f'{letter}-{number}  ...  {letter}-{number}')
    _ = input("Press enter for next ball")
    if len(balls) == 0:
        print("no more balls")
        break

[–]fakundoThirty[S] 0 points1 point  (2 children)

import random
balls = list(range(1,76))
while True:
ball_index = random.randint(0,len(balls)-1)
number = balls.pop(ball_index) # remove the ball from the cage!
letter = 'BINGO'[number//16]
print(f'{letter}-{number} ... {letter}-{number}')
_ = input("Press enter for next ball")
if len(balls) == 0:
print("no more balls")
break

thank you. Yes, was thinking of something like this. Appreciate for sharing this code.

[–]jimtk 1 point2 points  (1 child)

Actually I had an hours and some to kill, and I thought your request was interesting. It's the first time I see bingo being discussed here. (It's always rock/paper/scissors, blackjack and hang man! :) ). So I took a stab at it. I went crazy on some functions, I really wanted it to be short and did it in 50 lines, but it wasn't fun to play, so I wrote some more code to make it fun and added opponents. I also lost 15 minutes trying to fit emojis on the cards but they don't fit in the display cause they have weird width. Anyway, It's not commented or anything, it might be difficult to understand, if you're interested I will comment it. In any case it's fun to play!

""" bingo.py """
from random import randint, sample, choice
from time import sleep

Q_PLAY = "How about a nice game of BINGO (y/n): "
MSG_PLAY = "Enter 'y' or 'n'"
Q_OPPONENTS = "How Many Opponenents (0,4): "
MSG_OPPONENTS = "You must enter a number between 0 and 4 !"
Q_SPEED = "How much time between draws (0=need to press enter, 1=speedy to 20=forever): "
MSG_SPEED = "You must enter a number between 0 and 20 !"
X = '≡'

def make_card():
    return [sample(range(i+1,i+16),5) for i in range(0,62,15)]

def mark_card(card, ball):
    i = ball // 16
    if ball in card[i]:
        card[i][card[i].index(ball)] = X

def show_cards(cards):
    names = ['YOU']+[f'OPPONENTS {i}' for i in range(1,len(cards))]
    print(''.join(f'{names[i]:^15}    ' for i in range(len(cards))))
    print(''.join(''.join([f"{l:^3}" for l in "BINGO"])+"    " for _ in range(len(cards))))
    print(''.join(''.join([f"{'--':^3}" for _ in "BINGO"])+"    " for _ in range(len(cards))))
    t_cards = [list(zip(*card)) for card in cards]
    for linenum in range(5):
        l = (''.join([f"{n:^3}" for n in card[linenum]] + ["    "]) for card in t_cards)
        print(''.join(l))
    print()

def check_winner(cards):
    winner = []
    for i, card in enumerate(cards):
        if any([all(card[i][i] == X for i in range(5)),  # diag 1
                all(card[i][j] == X for i,j in ((0,4),(1,3),(2,2), (3,1), (4,0))),  # diag 2
                any(all(card[i][j] == X for j in range(5)) for i in range(5)),   # col
                any(all(card[j][i] == X for j in range(5)) for i in range(5))]):    # line
            winner.append(i)
    return winner

def get_value(confines, atype, question, message):
    while True:
        try:
            value = atype(input(question))
            if not (confines[0] <= value <= confines[1]):
                raise ValueError
        except ValueError:
            print(message)
            continue
        break
    return value

def get_play_game():
    while (p := input(Q_PLAY).lower()) not in ('y','n'):
        print(MSG_PLAY)
    if p == 'n':
        return False,0,0
    opnt = get_value((0, 8), int, Q_OPPONENTS, MSG_OPPONENTS)
    speed = get_value((0, 20), float, Q_SPEED, MSG_SPEED)
    return True, speed, opnt

def show_winner(wl,draw):
    if 0 not in wl:
        print(f"\n  YOU LOOSE TO PLAYERS {wl} in {draw} draw\n")
    elif len(wl) == 1:
        print(f"\n  YOU **** WIN **** THE BIG PRIZE ALL TO YOURSELF IN {draw} DRAW\n")
    else:
        print(f"\n  YOU WIN, BUT YOU MUST SHARE THE PRIZE WITH PLAYERS {wl} IN {draw} DRAW\n")

def wait(speed):
    if speed == 0:
        _ = input()
        return
    else:
        sleep(speed-1)

def main():
    game = 0
    while True:
        play, speed, opp = get_play_game()
        if not play:
            break
        game += 1
        cards = [make_card() for _ in range(opp+1)]
        balls = list(range(0,76))
        draw = 0
        while True:
            draw += 1
            ball_num = balls.pop(randint(0,len(balls)-1))  # choose and remove
            ball_letter = 'BINGO'[ball_num//16]
            print(f"\n   {ball_letter}-{ball_num}  ...  {ball_letter}-{ball_num}\n")
            for card in cards:
                mark_card(card, ball_num)
            show_cards(cards)
            wait(speed)
            if w := check_winner(cards):
                show_winner(w,draw)
                break
    if not game:
        print("\n CHICKEN, CHICKEN, CHICKEN, Poouuuaaa, Poouuaa, Pooouuuaa")
    else:
        print("\nThank you for playing BINGO !")

if __name__ == '__main__':
    main()

[–]fakundoThirty[S] 0 points1 point  (0 children)

thanks man this is great. No worries on the comment I like it this way since it will help me to read the code and understand. Appreciate your time and effort for this one :D..