

For a more idiomatic class-based solution, you should probably prefer answer (or use your existing solution) and for a more idiomatic generator-based solution, you should probably prefer answer. It is a little pointless to combine the two in this way, however.
Class bingo generator generator#
Here's a solution that "uses a generator and a class", as was requested in the question. With a generator, so I was wondering if that was possible with the class itself or if it is a completely different approach. However, I have been asked to do it this way bg = Bingo(100) In order to extract a ball from my Bingo, I would need to do the below: bg = Bingo(100) Self.picked_balls.append(self.current_ball) Random.choice([i for i in range(1, self.initial_size) # Do not consider the already picked balls for choosing randomly Provided, what are the balls that have been taken out since the startĪnd how many balls are remaining. Once created, every time a ball is taken out, the ball number must be # just start with a plain dictionary, random.You need to create a Bingo generator in which the size can be _min and _max are integers that default to 1 and 15 The generate_card method can be done on _init_, and everything else takes a self rather than a card: The fact that you're passing card around to a lot of functions says (to me) that you could probably use a class here. Values is an iterable over either rows or columns Looking at this a bit further, I'd probably refactor the row and column check into a different function: def check_line(values): This is called short-circuiting, meaning that you only evaluate up to the desired condition. Print(f"\nNumber drawn: įor idx, key in zip(reversed(range(3)), d):Īlso, the return True on all of the conditions means that the function will stop once a winning condition is hit, rather than going through all of the conditions when you really don't need to. Number_drawn = draw(card, random_draw_list) While win = False and user_input != "quit": Print("\nKeep pressing enter to continue or type quit to exit.\n") Generates a card, prints it, and loops through drawingĪnd printing until the check_win method returns True or the user enters "quit". If card = "X" and card = "X" and card = "X" and card = "X" and card = "X":Įlif card = "X" and card = "X" and card = "X" and card = "X" and card = "X":Įlif card = "X" and card = "X" and card = "X" and card = "X": List (list): The list of random numbers to be drawn from.įirst checks for diagonal wins, then four-corner, then horizontal, and finally, vertical.Ĭard (dictionary): The card to check for a win. Using the pop method ensures no duplicateĬard (dictionary): The card to to check for the number that was drawn. Pops a number off a list of random numbers. Generates a bingo card and stores the numbers in a dictionary.Ĭard = random.sample(range(min, max), 5)Ĭard (dictionary): The card to be printed out. Random_draw_list = random.sample(range(1, 76), 75)
Class bingo generator code#
I went above and beyond what the assignment called for so I need a little more helping making sure the code is okay.

This is for a class, and my professor is a stickler for using best practices. I am posting it here to make get some help reviewing it to make sure I'm using the best practices. I am super proud of this python bingo game I wrote.
