1

I am building a function that takes a list made up of lists (ex: [['a'],['b'],['c']]) and outputs it as a table. I cannot use pretty table because I need a specific output (ex | a | b | ) with the lines and the spaces exactly alike.

Here is my function:

def show_table(table):
  if table is None:
    table=[]
    new_table=""
    for row in range(table):
       for val in row:
         new_table+= ("| "+val+" ")
    new_table+= "|\n"
  return new_table.strip("\n")

I keep getting the error:

show_table([['a'],['b'],['c']])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in show_table
TypeError: 'list' object cannot be interpreted as an integer

I'm not sure why there is an issue. I've also gotten an output error where it only outputs the first item in the first list and nothing more. Could someone explain how to use the format function to get rid of this error and output what I want correctly?

Fixed error but still failing tests:

FAIL: test_show_table_12 (main.AllTests)

Traceback (most recent call last):
  File "testerl7.py", line 116, in test_show_table_12
    def test_show_table_12 (self): self.assertEqual (show_table([['10','2','300'],['4000','50','60'],['7','800','90000']]),'| 10   | 2   | 300   |\n| 4000 | 50  | 60    |\n| 7    | 800 | 90000 |\n')
AssertionError: '| 10| 2| 300|\n| 4000| 50| 60|\n| 7| 800| 90000|' != '| 10   | 2   | 300   |\n| 4000 | 50  | 60    |\n| 7    | 800 | 90000 |\n'
- | 10| 2| 300|
+ | 10   | 2   | 300   |
?     +++   +++     +++
- | 4000| 50| 60|
+ | 4000 | 50  | 60    |
?       +    ++    ++++
- | 7| 800| 90000|+ | 7    | 800 | 90000 |
?    ++++     +       + +
4
  • Please include the exact error message you receive for a given input to your function … "list is not iterable" is not it. Also ensure that the code you've posted here is the exact code you're having problems with; as written, the function above will never output anything at all, so it can't be outputting the first item in the list. Commented Mar 18, 2017 at 20:54
  • I included the exact error I'm now getting @ZeroPiraeus Commented Mar 18, 2017 at 21:03
  • 1
    Please review how to format your posting in the help files. Commented Mar 18, 2017 at 21:20
  • Instructions on how to format your question properly (including code etc.) are available here. Commented Mar 18, 2017 at 21:23

2 Answers 2

2

The problem is here:

for row in range(table):

range takes 1, 2, or 3 integers as arguments. It does not take a list.

You want to use:

for row in table:

Also, check your indents; it looks like the newline addition should be indented more.

Sign up to request clarification or add additional context in comments.

3 Comments

Fixed the output! But I am still failing test. I added the failed test to the question. Please take a look. @Tom Zych
You should try to solve the formatting issue yourself, now that you have code that will execute, and post another question if you're still stuck. Don't load more stuff onto an answered question.
I am working on that as ya'll have been responding. I didn't think it was worth asking another question.
1

Your traceback tells you that the problem occurs on line 5:

for row in range(table):

… so something on that line is trying, without success, to interpret something else as an integer. If we take a look at the docs for range(), we see this:

The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method).

… but table is not an integer; it's a list. If you want to iterate over a list (or something similar), you don't need a special function – simply

for row in range:

will work just fine.

There's another problem with your function apart from the misuse of range(), which is that you've indented too much of your code. This:

  if table is None:
    table=[]
    new_table=""
    for row in range(table):
       for val in row:
         new_table+= ("| "+val+" ")
    new_table+= "|\n"

… will only execute any of the indented code if table is None, whereas what you really want is just to set table=[] if that is the case. Fixing up both those problems gives you this:

def show_table(table):
    if table is None:
        table=[]
    new_table = ""
    for row in table:
        for val in row:
             new_table += ("| " + val + " ")
        new_table += "|\n"
    return new_table.strip("\n")

(I've also changed all your indents to four spaces, and added spaces here and there, to improve the style).

6 Comments

You're a genius! That fixed the error issue. It still is not passing tests. I think I am missing the new lines...
Yes, the code above still doesn't format items as expected. Updating ...
Is there a way to use the format function to add in the new line?
Actually @Kelsey, now that I look at it, the formatting issue is a bit much to address in a single answer. Now that you have the basic structure of your program, I recommend asking a new question about it (hint: ask how to justify columns in table output) … and remember to read through the editing help so that your question displays correctly.
Okay! Thanks guys!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.