2
votes

so my code is designed to basically create a bingo card, then when you press enter draws a number and changes the bingo card value to 0 if the drawn number matches. My issue is that my code isn't properly producing a bingo or not a bingo which you can see towards the end of the code. How should I change this so that my code will return bingo when I have bingo.

import random #Condernsed solo version with call and player together
import numpy as np
def bingo_card(): #Code to generate BINGO card using numpy array

    test1=np.array(random.sample(range(1,16),5))
    test2=np.array(random.sample(range(16,31),5))
    test3=np.array(random.sample(range(31,46),5))
    test4=np.array(random.sample(range(46,61),5))
    test5=np.array(random.sample(range(61,76),5))

    test= np.concatenate((test1,test2,test3,test4,test5))
    test= test.reshape(5,5)
    test[2,2]=0
    bingo_card.test = test

    BINGO= print("Your Card")
    print("B", test[0]),
    print("I", test[1]), 
    print("N", test[2]), 
    print("G", test[3]), 
    print("O", test[4])
    card = BINGO
    
    return card
def called_value(): #should be close to the code we use to check if called value is in generated matrix, may need some formatting and fixing
    checked_card = bingo_card.test
    while True:
        called_num = input ("Type called number to mark card or press 's' to quit if you think you have bingo").lower()
        if called_num != "s":
            number_check = int(called_num)
            checked_card = np.where(checked_card==number_check,0,checked_card)
            BINGO = print("Your Card")
            print("B", checked_card[0]),
            print("I", checked_card[1]), 
            print("N", checked_card[2]), 
            print("G", checked_card[3]), 
            print("O", checked_card[4])
        elif called_num == "s":
            BINGO = print("Your Card")
            print("B", checked_card[0]),
            print("I", checked_card[1]), 
            print("N", checked_card[2]), 
            print("G", checked_card[3]), 
            print("O", checked_card[4])
            row_zeros = np.count_nonzero(checked_card == 0, axis=1)
            col_zeros = np.count_nonzero(checked_card == 0, axis=0)
            diagonal_zeros = np.count_nonzero(np.diag(checked_card) == 0)
            diagonal1_zeros = np.count_nonzero(np.diag(np.fliplr(checked_card)) == 0)
            for i in range (5):
                if (row_zeros[i] or col_zeros[i]) == 5:
                    return ("Bingo!")
                elif (diagonal_zeros or diagonal1_zeros) == 5:
                    return ("Bingo!")
                else:
                    return ("Not Bingo")
def solo():
    bingo_card()
    called_value()
solo()
1

1 Answers

1
votes

You've committed one of the classic blunders.

            for i in range (5):
                if (row_zeros[i] or col_zeros[i]) == 5:
                    return ("Bingo!")
                elif (diagonal_zeros or diagonal1_zeros) == 5:
                    return ("Bingo!")
                else:
                    return ("Not Bingo")

row_zeros[i] or col_zeros[i] is going to be True or False (usually True), which is never equal to 5. And you don't need to loop to check the diagonals. You need:

            if diagonal_zeros == 5 or diagonal1_zeros == 5:
                return ("Bingo!")
            for i in range (5):
                if row_zeros[i] == 5 or col_zeros[i] == 5:
                    return ("Bingo!")
            return ("Not Bingo")