In: Computer Science
Python
1. A salesman can sell five different items. Write a program that lets the salesman enter the quantity of each item sold, calculates the total sales, and prints as below. Use a for loop to ask the salesman how many of each product he sold.
tem 1 $2.50
Item 2 $1.98
Item 3 $5.75
Item 4 $3.45
Item 5 $4.00
2. Rewrite program #1 using a for loop to run for 3 salesmen and print the total sales for each
salesman as below.
3. Rewrite program #2 to let the salesman determine when he/she is finished entering the
item/quantity.
1.
CODE
prices = []
quantity = []
for i in range(1, 6):
quantity.append(int(input("Quantity sold for item %d? " %i)))
prices.append(float(input("Price of item %d? " %i)))
for i in range(5):
totalCost = prices[i] * quantity[i]
print("Item %d $%.2f" %(i+1, totalCost))
2.
CODE
for j in range(1, 4):
print("Enter details of Salesperson %d" %(j))
prices = []
quantity = []
for i in range(1, 6):
quantity.append(int(input("Quantity sold for item %d? " %i)))
prices.append(float(input("Price of item %d? " %i)))
for i in range(5):
totalCost = prices[i] * quantity[i]
print("Item %d $%.2f" %(i+1, totalCost))
3.
CODE
for j in range(1, 4):
print("Enter details of Salesperson %d" %(j))
prices = []
quantity = []
i = 1
while(True):
quantity.append(int(input("Quantity sold for item %d? " %i)))
prices.append(float(input("Price of item %d? " %i)))
i += 1
cont = input("Do you wish to continue? (yes/no) : ")
if cont.lower() == "no":
break;
for i in range(len(quantity)):
totalCost = prices[i] * quantity[i]
print("Item %d $%.2f" %(i+1, totalCost))