Home > Mobile >  Issue with pretty tables in python
Issue with pretty tables in python

Time:11-10

I'm currently working on a assignment for school, Im not asking for anyone to solve the problem for me, Im just trying to figure out what it is im doing wrong with pretty tables. Here is the code I have for the table itself

import random
from prettytable import PrettyTable
x = PrettyTable
def generate_bingo_card():
    dict = {'B': [], 'I': [], 'N': [], 'G': [], 'O': []}
    counter = 0
    while True:
        if counter == 5:
            break
        else:
            randb = random.randint(1, 15)
            randi = random.randint(16, 30)
            randn = random.randint(31, 45)
            randg = random.randint(46, 60)
            rando = random.randint(61, 75)

            dict['B'].append(randb)
            dict['I'].append(randi)
            dict['N'].append(randn)
            dict['G'].append(randg)
            dict['O'].append(rando)
            counter  = 1
    return dict

def print_bingo_card(card):
    x.add_column('B', [card['B']])
    x.add_column('I', [card['I']])
    x.add_column('N', [card['N']])
    x.add_column('G', [card['G']])
    x.add_column('O', [card['O']])

print(print_bingo_card(generate_bingo_card()))

and here is the error im getting

  File "C:\Users\Joshu\OneDrive\Desktop\Python assignment 3\a3q6_bingo.py", line 26, in print_bingo_card
    x.add_column('B', [card['B']])
TypeError: PrettyTable.add_column() missing 1 required positional argument: 'column'

I've followed the documentation to a tee and am still getting this error, If someone could point me in the right direction that would be great!

CodePudding user response:

I think this is a much more elegant solution; you have added complexity that I don't feel needs to exist; you can always retrofit this to your solution;

import prettytable
import random

def create_bingo_card():
    card = {}
    card['B'] = random.sample(range(1, 16), 5)
    card['I'] = random.sample(range(16, 31), 5)
    card['N'] = random.sample(range(31, 46), 5)
    card['G'] = random.sample(range(46, 61), 5)
    card['O'] = random.sample(range(61, 76), 5)
    card['N'] = random.sample(range(76, 95), 5)

     # create a pretty table
    pt = prettytable.PrettyTable(field_names=['B', 'I', 'N', 'G', 'O'])
    for row in zip(*[card['B'], card['I'], card['N'], card['G'], card['O']]):
        pt.add_row(row)
    return pt


card = create_bingo_card()
print(card)

Output:

 ---- ---- ---- ---- ---- 
| B  | I  | N  | G  | O  |
 ---- ---- ---- ---- ---- 
| 1  | 17 | 37 | 54 | 74 |
| 5  | 21 | 33 | 57 | 64 |
| 6  | 24 | 78 | 60 | 71 |
| 10 | 19 | 44 | 47 | 69 |
| 2  | 30 | 41 | 52 | 62 |
 ---- ---- ---- ---- ---- 
  • Related