I am trying to create a class that can act as a frequency table for a calculator project. I am having issues with printing the table in a nicely formatted way. Here is my current code:
class FrequencyTable:
def __init__(self, data: list, freq: list):
self.data = data
self.freq = freq
self.table_as_dict = {}
for item in self.data:
item = str(item)
self.table_as_dict[item] = self.freq
def print(self):
print("|Item|Frequency|")
print("|--------------|")
for item in self.table_as_dict:
item = str(item)
while True:
if f"{str(item)}" < 4:
item = f"{item} "
else:
break
r_item = self.table_as_dict[item]
while True:
if len(item) < 9:
r_item = f"{r_item} "
else:
break
print(f"|{item}|{r_item}|")
The print function works until the first if statement in the while loop, which throws the exception
TypeError: object of type 'int' has no len() e.g
table = FrequencyTable([1,2,3], [7,8,9])
table.table_as_dict would be {1: 7, 2: 8, 3: 9}. If i called print, I would get:
|Item|Frequency|
|--------------|
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "filepath", line 14, in print
item = str(item)
How can I get the print function to print all of the data whilst keeping it's formatting?