In: Computer Science
This is for Python programming, and I am trying to use 2 for loops to ask a slaesperson how many of 5 different items they sold, then the program is to calculate the total dollar amount sold. Formatting and all that aside, I am having an issue with the loops not stepping through the 2 lists at the same time. The first loop is taking all 5 entered quantities times the price of the first item, and then all 5 quantities times the price of the 2nd item, etc. I need to get Item1 * Quant1, Item2 * Quant2, etc.
Item1 = 2.5
Item2 = 1.98
Item3 = 5.75
Item4 = 3.45
Item5 = 4.0
Quant1 = int(input("Enter the quantity of Item1 sold: "))
Quant2 = int(input("Enter the quantity of Item2 sold: "))
Quant3 = int(input("Enter the quantity of Item3 sold: "))
Quant4 = int(input("Enter the quantity of Item4 sold: "))
Quant5 = int(input("Enter the quantity of Item5 sold: "))
for Item in [Item1, Item2, Item3, Item4, Item5]:
for Quant in [Quant1, Quant2, Quant3, Quant4, Quant5]:
Cost = float(Quant * Item)
print (Cost)
Python 3 code
============================================================================================
Item1 = 2.5
Item2 = 1.98
Item3 = 5.75
Item4 = 3.45
Item5 = 4.0
Quant1 = int(input("Enter the quantity of Item1 sold: "))
Quant2 = int(input("Enter the quantity of Item2 sold: "))
Quant3 = int(input("Enter the quantity of Item3 sold: "))
Quant4 = int(input("Enter the quantity of Item4 sold: "))
Quant5 = int(input("Enter the quantity of Item5 sold: "))
total=0
for Item,Quant in zip([Item1, Item2, Item3, Item4, Item5],[Quant1,
Quant2, Quant3, Quant4, Quant5]):
Cost=float(Quant * Item)
total=total+Cost
print(Cost)
print('total cost is: ',total)
============================================================================================
Output