Python. 1. Write a script called that repeatedly asks the user to first enter a
ID: 3667396 • Letter: P
Question
Python.
1. Write a script called that repeatedly asks the user to first enter a price and then a number of items for that price. At any point, the user can type in done (case insensitive) to indicate that they are finished entering items, and the script should display the total cost and the total number of items. It should keeps track of the item numbers.
Example:
2. Improve the script so that it handles situations where the user enters in something other than a number (or the word done) without crashing or ending the whole thing early. It can't crash and it can't just give up entirely when the user enters in non-numeric data.
RESTART At any time, you may type 'done' to finish. What is the c st f Item #1? 1.50 How many of item #1 are you buying? What is the c st f item #2? How many of item #2 are you buying? 10 What is the c st f item #3? done You have purchased 12 items , at a tta1 c stf $33.0 RESTART At any time, you may type 'done' to finish What is the c st f item #1? 1.99 How many of item #1 are you buying? >100 What is the c st of item #2? >7.01 How many of item #2 are you buying? >DONE You have purchased 100 items , at a total cst f $199.0 RESTART At any time, you may type 'done' to finish What is the c st f item #1? >Done You have purchased 0 items, at a total cost of $0Explanation / Answer
Solution:
1. Script 1: See the code below
--------------------------------------------
# Script to repeatedly ask price of an item, no. of items to be purchased
# and display total items purchased and total cost, when done.
print("At any time, you may type 'done' to finish.")
counter = 0 #counter to track types of items
total_items = 0 #total items purchased
total_cost = 0 #total cost of items purchased
#main loop for user interaction
while 1:
counter = counter+1
print("What is the cost of item #"+str(counter)+"?")
cost=input(">")
#breaks loop, when done
if cost == "done":
break
print("How many of item #"+str(counter)+" are you buying?")
items=input(">")
#calculates
total_items=total_items+int(items)
total_cost=total_cost+(float(cost)*int(items))
print("You have purchased "+str(total_items)+" items, at a total cost of $"+str(total_cost)+".")
--------------------------------------
2. Modified script: See modified script below:
----------------------------------------------------
# Script to repeatedly ask price of an item, no. of items to be purchased
# and display total items purchased and total cost, when done.
print("At any time, you may type 'done' to finish.")
counter = 1 #counter to track types of items
total_items = 0 #total items purchased
total_cost = 0 #total cost of items purchased
#main loop for user interaction
while 1:
print("What is the cost of item #"+str(counter)+"?")
cost=input(">")
#checks whether proper value is entered or not
if cost.isdigit():
print("How many of item #"+str(counter)+" are you buying?")
items=input(">")
#calculates total items and cost
total_items=total_items+int(items)
total_cost=total_cost+(float(cost)*int(items))
counter = counter+1
elif cost == "done":
#breaks loop, when done
break
else:
print("Wrong format! Re-enter.")
#prints final message
print("You have purchased "+str(total_items)+" items, at a total cost of $"+str(total_cost)+".")
-----------------------------------------------
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.