Mattias Zurkovic
Mattias Zurkovic

Reputation: 333

Python stdout and for loop won't print the first element in a list (Second element replaces the first))

I am creating a program that has the user enter a list and then generates a table from that list. The elements in that list (for the new_table function) will become the the label for the columns.

from sys import stdout

def new_table(columns):
    for i in columns:
        length = len(i)
        stdout.write('-' * (length + 8))
    print('\r')
    for x in columns:
        stdout.write('|   ' + i + '   |')

new_table(['Animal', 'Danger'])

However, when I run the program, in stead of displaying both elements of the list as the column titles/labels it displays only the second for both titles (the second element replaces the first):

----------------------------
|   Danger   ||   Danger   |

I would like it to display like this:

----------------------------
|   Animal   ||   Danger   |

Thanks for the help and suggestions.

Upvotes: 0

Views: 86

Answers (1)

Karoly Horvath
Karoly Horvath

Reputation: 96266

Consistency helps. You loop with x but use i.

Upvotes: 3

Related Questions