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 |
?    ++++     +       + +
Tom Zych
  • 13,329
  • 9
  • 36
  • 53
Kelsey Greenwood
  • 67
  • 1
  • 1
  • 10
  • 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. – Zero Piraeus Mar 18 '17 at 20:54
  • I included the exact error I'm now getting @ZeroPiraeus – Kelsey Greenwood Mar 18 '17 at 21:03
  • 1
    Please review how to format your posting in the help files. – Tom Zych Mar 18 '17 at 21:20
  • Instructions on how to format your question properly (including code etc.) are available [here](http://stackoverflow.com/editing-help). – Zero Piraeus Mar 18 '17 at 21:23

2 Answers2

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.

Tom Zych
  • 13,329
  • 9
  • 36
  • 53
  • Fixed the output! But I am still failing test. I added the failed test to the question. Please take a look. @Tom Zych – Kelsey Greenwood Mar 18 '17 at 21:18
  • 1
    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. – Tom Zych Mar 18 '17 at 21:24
  • I am working on that as ya'll have been responding. I didn't think it was worth asking another question. – Kelsey Greenwood Mar 18 '17 at 21:29
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).

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • You're a genius! That fixed the error issue. It still is not passing tests. I think I am missing the new lines... – Kelsey Greenwood Mar 18 '17 at 21:26
  • Yes, the code above still doesn't format items as expected. Updating ... – Zero Piraeus Mar 18 '17 at 21:27
  • Is there a way to use the format function to add in the new line? – Kelsey Greenwood Mar 18 '17 at 21:27
  • 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](http://stackoverflow.com/editing-help#code) so that your question displays correctly. – Zero Piraeus Mar 18 '17 at 21:49
  • Okay! Thanks guys! – Kelsey Greenwood Mar 18 '17 at 21:50
  • Oh, and by the way, your problem isn't about newlines … try `print(show_table([['10','2','300'],['4000','50','60'],['7','800','90000']]))` and `print()` to see what's actually tripping up the test. – Zero Piraeus Mar 18 '17 at 21:54