Home > Enterprise >  Put an append list into a table with python
Put an append list into a table with python

Time:11-05

I'm trying to put my append list into a table. I only can print one line of the list but not the others since the amount of numbers and words can varie from 1 to infinity

everthing = []
option = 0

while option < 5:
    Mainmenue = ["Main menue", "1. ADD AN ITEM", "4. GENERATE A REPORT"]

    for i in (Mainmenue):
        print(i)
    option = int(input("Option you want:"))

    if option == 1:
       item = (input("add an item:"))
       itemquantity = (input("Item quantity:"))
       everthing.append(item)
       everthing.append(itemquantity)
       print(everthing)
       
   
   

    elif option == 4:
      #Here is the question
      for thing in everthing:
       print(thing, sep='\t')  
      #Here is the question end

      Mainmenue = ["Main menue", "1. ADD AN ITEM","4. GENERATE A REPORT"]

      for i in (Mainmenue):
        print(i)
        option = float(input("Option you want:"))

When I run this code to give me a list it gives the table like this

Jam
40
Sprite
30

But I want it to be like this

Jam    40

Sprite 30

Thanks in advance :)

CodePudding user response:

You can use zip to get pair of item and quantity from everything list with index slicing:

#Here is the question
for item, quantity in zip(everything[0::2], everything[1::2]):
    print(f'{item} \t{quantity}')
#Here is the question end

Output:

Jam     40
Sprite  30

CodePudding user response:

You were very close. However, you do have some redundant code.
I added a 5. QUIT option to clarify the method to exit the program.

I tried to keep it within your specifications, however you may want to consider some error checking code to handle the issue if they do not enter a number when asked to select an option from the menu.

But here is a version of your program, keeping within the specifications:

everthing = []
option = 0

while option < 5:
    Mainmenue = ["Main menue", 
                 "1. ADD AN ITEM", 
                 "4. GENERATE A REPORT",
                 "5. QUIT"]

    for i in (Mainmenue):
        print(i)
    option = int(input("Option you want:"))

    if option == 1:
        item = (input("add an item:"))
        itemquantity = (input("Item quantity:"))
        item_added = f"{item}\t{itemquantity}"
        everthing.append(item_added)
        print("Added:", item_added, "\n")
        
    elif option == 4:
        # Generate the report
        print("\n---------REPORT---------")
        for thing in everthing:
            print(thing)  
        print("------------------------\n")

Here are the selections I made:

  • 1 : Option 1 (add an item)
  • Jam
  • 40
  • 1 : Option 1 (add an item)
  • Sprite
  • 30
  • 4 : Option 4 (generate report)
  • 5 : Option 5 (quit)

OUTPUT:

Main menue
1. ADD AN ITEM
4. GENERATE A REPORT
5. QUIT
Added: Jam  40 

Main menue
1. ADD AN ITEM
4. GENERATE A REPORT
5. QUIT
Added: Sprite   30 

Main menue
1. ADD AN ITEM
4. GENERATE A REPORT
5. QUIT

---------REPORT---------
Jam     40
Sprite  30
------------------------

Main menue
1. ADD AN ITEM
4. GENERATE A REPORT
5. QUIT

While I noticed you used a tab to separate the items from the qty, you may want to use padding instead. This will ensure the quantities are more likely to line up with each other and are less dependent on the length of the item (ie. number of characters).

Here is what I would suggest in terms of padding.
I would change the code for option 1 to the following:

    if option == 1:
        padding = 21
        item = (input("add an item:")).ljust(padding)
        itemquantity = (input("Item quantity:"))
        item_added = f"{item}{itemquantity}"
        everthing.append(item_added)
        print("Added:", item_added, "\n")

You can see that I have included a padding variable, that allows up to 21 characters for the item variable using the .ljust() function.

The report ends up looking like this:

---------REPORT---------
Jam                  40
Sprite               30
------------------------
  • Related