Use Python Based on chapter 4 Loops Write a program to proc
Use Python. Based on chapter 4 - Loops: Write a program to process items that a user purchases.
a) We like to be able to have the user enter prices for as many items that she wants to purchase. Allow for a sentinel value to end the input loop. Select an appropriate sentinel value based on the data you’re processing. Avoid the use of Yes/NO or DONE input to end the data input loop. If the user chooses not to enter any data at all, issue an error message and do not calculate/display any output.
b) Do not allow the user to enter an invalid price (A number greater than 1000). Issue an error message and allow the user to enter a valid price.
c) Calculate and display the total amount of all purchases and the number of items purchased.
d) Calculate and display the total amount with an applied 8% tax rate. e) Calculate and display the average price of all purchases after the application of tax
Solution
count = 0
total = 0
print \"Enter 0 to stop input\"
# get input from user and calculate the sum of costs.
while True:
try:
item = input(\"Item {0} Price:\".format(count+1))
item = int(item)
if(item==0):
break
if(item>1000): # if price>1000 skip the rest of the steps and continue the loop
print \"Invalid Price\"
continue
total += item
count += 1
print item
except:
print \"Invalid Price\"
break
# input is empty
if(not count):
print \"No valid prices entered.\"
else:
print \"Purchaced goods : \",count
print \"Total cost : \",total
total += (total*8)/100.0
print \"Cost with tax : \",total
print \"Average cost : \",(total/count)
#sample output
