0

I am learning how the classes work, but I have encountered a problem that had appeared to me on other occasions.

The question is that I want to have a class to which two parameters are passed: columns and rows. And that by calling a method inside the class, that table is automatically generated.

class Tables:
    def __init__(self, rows, columns):
        self.rows = rows
        self.columns = columns

    def generate_T(self):
        table = BeautifulTable()
        for i in range(self.rows):
            table.rows.append([""])
        
            
        print(table)

The table.rows.append method requires that for each column this is inserted: "",. I try this:

columns = "\"\","*self.columns
for i in range(self.rows):
    table.rows.append([columns[:-1])

It does not work, since it is taken as a single text string, I have also tried to remove the comma from the function, but then some parentheses appear. Is there any method to fix this? Greetings and thanks

P.D: To be clear: table.rows.append (["", "", ""])

progmatico
  • 4,714
  • 1
  • 16
  • 27
Emel
  • 2,283
  • 1
  • 7
  • 18
  • 2
    Does this answer your question? [Create list of single item repeated N times](https://stackoverflow.com/questions/3459098/create-list-of-single-item-repeated-n-times). Basically, you want `table.rows.append([""] * self.columns)`. – Brian McCutchon Dec 15 '20 at 19:45
  • That work's perfect. Thanks – Emel Dec 15 '20 at 21:23
  • But I don't undestarnd how it works. Why It doesn't return `["]["]["]...`? – Emel Dec 15 '20 at 21:30
  • 1
    Because it is a list and not a string. Conversely, in your question, you used a string when you should have used a list. – Brian McCutchon Dec 15 '20 at 23:21

0 Answers0