1

I just want to print some formatted output but I don't understand so I need help. Here is my code.

In list,

[dev, 1, 1999-09-08, y]
[res, 2, 2000-01-02, n]

...

Above list is inputted by user.

    inputlist = []
    devname = input("Device Name: ")
    devcount = input("Device Count: ")
    devdate = input("Production Date(ex: 1990-01-01): ")
    devres = input("Stock? (y/n): ")
    inputlist.append(devname+","+devcount+","+devdate+","+devres)

And now, I want to print above data.

dev    1     1999-09-08    y
res    2     2000-01-02    n

I've tried to for iteration and print but I don't have idea anymore.

    for i in range(len(inputlist)):
        rd = inputlist[i].split(',')
        for j in range(len(rd)):
            print(rd[j], sep='\t', end='')

Anyone help for me? I'm learning python at the very beginning so I don't understand very well about for iteration... Thank you very much in advance!

Lucy Kim
  • 35
  • 6

2 Answers2

0

Solution

The best option for printing data in a tabular table is at Printing Lists as Tabular Data


Alternative

Your lists are,

list_a = [dev, 1, 1999-09-08, y]
list_b = [res, 2, 2000-01-02, n]`

The way I would recommend would be turn them into one list which would look like:

>>> listm = [a, b]
>>> listm
[[dev, 1, 1999-09-08, y], [res, 2, 2000-01-02, n]]

You can then itterate through the list while selecting which column of elements you want to print.

for li in listm:
   print("{}\t{}\t{}\t{}".format(li[0], li[1], li[2], li[3]))

or

for li in listm:
    print(*li, sep='\t')

Result:

dev    1     1999-09-08    y
res    2     2000-01-02    n
Max Collier
  • 573
  • 8
  • 24
  • I have idea for your comment. Thank you for your advice! – Lucy Kim Feb 12 '18 at 01:28
  • No problem! I recommend that when you program you should look to make your code as simple as possible to understand and debug. This may mean using naming conventions that explain somewhat what the item is rather than using placeholder naming conventions like i and j. That made me a little confused with your code. Anyway good luck with your programming! – Max Collier Feb 12 '18 at 01:40
0

You just got the two parameters mixed up!

Try this for your last line:

print(rd[j], sep='', end='\t')
abuv
  • 169
  • 1
  • 1
  • 13