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
------------------------