0

I've been trying to figure out how to do bingo cards in python. I just learned about def functions and required to use them within my code. The point of this program is to Write a program that will create from 1 to n Bingo cards where n is some integer between 1 and 10. Each Bingo card consists of 5 columns and every column has a unique set of numbers for a given range. The ranges consist of the following: B – integers from 1 to 15 I – integers from 16 to 30 N – integers from 31 to 45 G – integers from 46 to 60 O – integers from 61 to 75 Each card is to be displayed with the BINGO heading along with 5 rows consisting of 5 B’s, 5 I’s, 4 N’s, 5 G’s and 5 O’s. There are only 4 N’s because the third row of the N column should display two dashes to indicate the free space on the card. What I have so far is this.

import random
GET_CARDNUMS = int(input('Enter the number of Bingo cards to generate (1-10): '))
    
def main():
  get_cardnums()
  generate_nums()
  sort_nums()

def get_cardnums():
  while True:
    get_cardnums in range (GET_CARDNUMS)
    if GET_CARDNUMS <= 10:
      break
    else:
      print ('Enter the number of Bingo cards to generate (1-10): ')
      break
      print()

def generate_nums():
  minB = 1
  maxB = 15
  for B in range (5):
    B = random.randint (minB, maxB)
    print (B)

  minI = 16
  maxI = 30
  for I in range (5):
    I = random.randint (minI, maxI)
    print (f'I{I}')

  minN = 31
  maxN = 45
  for N in range (4):
    N = random.randint (minN, maxN)
    print (f'N{N}')

  minG = 46
  maxG = 60
  for G in range (5):
    G = random.randint (minG, maxG)
    print (f'G{G}')

  minO = 61
  maxO = 75
  for O in range (5):
    O = random.randint (minO, maxO)
    print (f'O{O}')

def sort_values():


main ()

I was trying to look up lists and sorting functions because he said it would be helpful in creating rows but I just can not figure out how to set it up. Also sorry for the f-strings. I was trying to figure out how to print B, I, N, G, and O just as a single letter at the top of the randomly generated number but it seems just to end up outputting 5 B's for each random number, 5 I's for each random number, and so forth. Should I just print each letter separately for each number? like B1= B2= B3=. When looking it up many people were saying to use dictionary but I have not learned that yet so I'd want to stay away from that.

edit:

import random

GET_CARDNUMS = int(input('Enter the number of Bingo cards to generate (1-10): '))

def main():
  get_cardnums()
  generate_nums()
  sort_nums()
  
def get_cardnums():
  while True:
    get_cardnums in range (GET_CARDNUMS)
    if GET_CARDNUMS <= 10:
      break
    else:
      print ('Enter the number of Bingo cards to generate (1-10): ')
      break
      print()

def generate_nums():
  B = sorted(random.sample(range(1, 16), 5))
  I = sorted(random.sample(range(16, 31), 5))
  N = sorted(random.sample(range(31, 46), 4))
  G = sorted(random.sample(range(46, 61), 5))
  O = sorted(random.sample(range(61, 76), 5))

def sort_nums():
  print (' B   I   N   G   O   ')
  print (f' {B:>3} {I:>3} {N:>3} {G:>3} {O:>3}')
  print (f' {B:>3} {I:>3} {N:>3} {G:>3} {O:>3}')
  print (f' {B:>3} {I:>3} --- {G:>3} {O:>3}')
  print (f' {B:>3} {I:>3} {N:>3} {G:>3} {O:>3}')
  print (f' {B:>3} {I:>3} {N:>3} {G:>3} {O:>3}')

main ()

Currently, my code will prompt with an error saying B I N G O is not defined. I'm not sure how to fix that.

Natawee
  • 17
  • 8
  • Have a look at the [`random.sample()`](https://docs.python.org/3/library/random.html#random.sample) function. For example: `B = sorted(random.sample(range(1, 16), 5))`. The way you are doing it now might create repeated values. – 001 Sep 29 '22 at 17:10
  • ahh makes sense I did get repeated numbers but then the numbers get output in rows instead of columns @JohnnyMopp – Natawee Sep 29 '22 at 17:24
  • That's ok. You can store the card however you want. Just make a function that prints the card in the right format. – 001 Sep 29 '22 at 17:42
  • I was trying to put it in my def generate_nums but it keeps saying B is not defined would I have to put it in my main def? @JohnnyMopp – Natawee Sep 29 '22 at 17:47
  • Hard to say without seeing the code. I through this together to show you one way to do it: https://ideone.com/dZ5e4q – 001 Sep 29 '22 at 17:57
  • BTW, you do NOT want to "sort" the values for each column!! – Tim Roberts Sep 29 '22 at 18:36
  • @JohnnyMopp Thank you!! I can't believe how small the code can get! could you take a look at the edit I made? If there are any pointers you could give off of my code that would be very appreciated. – Natawee Sep 29 '22 at 18:36
  • The variables B, I, N, G, and O are local to the `generate_nums` function. They go away when the function returns. Perhaps you should return them as a list of 5 things. – Tim Roberts Sep 29 '22 at 18:37
  • @TimRoberts I'm sorry could you explain a bit more I am confused. I guess I am more so looking to create columns not sort the values or numbers. – Natawee Sep 29 '22 at 18:39
  • @TimRoberts Yeah I heard lists would help but I am not even sure how I would set the list up with letters instead of numbers. I haven't done lists before in general so I am not quite sure how to execute it. – Natawee Sep 29 '22 at 18:44
  • Variables have a "scope" - kind of like a lifespan. Outside of that scope, they don't exist. You could `return` the variables but since you say you are not familiar with lists, another option is to make them 'global'. Make the first line of the `generate_nums` function: `global B,I,N,G,O`. See also: [UnboundLocalError on local variable when reassigned after first use](https://stackoverflow.com/q/370357) – 001 Sep 29 '22 at 18:55
  • @JohnnyMopp Yeah I did a global variable for the input of cards so it makes sense but I do want to figure out how to apply a list. the formate I was given was def sort_nums(num1, num2, num3, num4, num5): # Create a list of sorted numbers num_list = sorted([num1, num2, num3, num4, num5]) return num_list[0], num_list[1], num_list[2], num_list[3], num_list[4] but how would I set that up with the way my code is set up I feel like I was have the separate each letter B1 B2 B3 in order for that to work (sorry I don't know how to separate code in the comment section) – Natawee Sep 29 '22 at 19:06

1 Answers1

0

Here's the 2nd version of your code with the fewest changes just to make it work. There's definitely room for improvement.

import random

def main():
  num_cards = get_cardnums()
  for _ in range(num_cards):
    card = generate_nums()
    print_nums(card)
  
def get_cardnums():
  while True:
    GET_CARDNUMS = int(input('Enter the number of Bingo cards to generate (1-10): '))
    if GET_CARDNUMS <= 10:
      return GET_CARDNUMS
   # TODO: What happens if user enters something that is not an integer?

def generate_nums():
  B = random.sample(range(1, 16), 5)
  I = random.sample(range(16, 31), 5)
  N = random.sample(range(31, 46), 4)
  G = random.sample(range(46, 61), 5)
  O = random.sample(range(61, 76), 5)
  return [B,I,N,G,O]

def print_nums(card):
  print ('   B   I   N   G   O   ')
  print (f' {card[0][0]:>3} {card[1][0]:>3} {card[2][0]:>3} {card[3][0]:>3} {card[4][0]:>3}')
  print (f' {card[0][1]:>3} {card[1][1]:>3} {card[2][1]:>3} {card[3][1]:>3} {card[4][1]:>3}')
  print (f' {card[0][2]:>3} {card[1][2]:>3} --- {card[3][2]:>3} {card[4][2]:>3}')
  print (f' {card[0][3]:>3} {card[1][3]:>3} {card[2][2]:>3} {card[3][3]:>3} {card[4][3]:>3}')
  print (f' {card[0][4]:>3} {card[1][4]:>3} {card[2][3]:>3} {card[3][4]:>3} {card[4][4]:>3}')

main ()

Note: I removed the sorting. I searched Google Images for bingo cards, and they were all unsorted.

001
  • 13,291
  • 5
  • 35
  • 66
  • Thank you so much, a question on the def print_nums what are the numbers in brackets representing? – Natawee Sep 29 '22 at 19:46
  • like {card[0][0] what would the zeros represent – Natawee Sep 29 '22 at 19:52
  • `card` is a 2-dimensional list (a list-of-lists). The first index is the column index (B, I, N, G, or O) and the 2nd index is the row (B-0, B-1, B-2, etc). So for example: `card[3][1]` would be column G, 2nd row. (Remember: indexes are 0-based.) – 001 Sep 29 '22 at 20:01
  • Okay, that makes a lot of sense now. This will probably be my last question but for def main I am honestly confused about the whole function. When you get num_cards to equal get cardnums() and for _ in range (num_card): What does that end up doing? – Natawee Sep 29 '22 at 20:08
  • The `get_cardnums()` function returns the number of cards to create. `for _ in range (num_card):` loops that many times, creating that many cards. Note: the underscore (`_`) is used to represent a throw-away variable. – 001 Sep 29 '22 at 20:11