[英]invoice (receipt) program in python. How to prevent overwriting old values
我是 python 的新學習者,我正在嘗試制作一個程序,在發票中打印所有物品 + 價格 + 數量。 每個項目都在單獨的行中。
我已經知道我在一行中打印每個項目,但我一直用最后輸入的值覆蓋舊值。 我怎樣才能防止這種情況? 這是代碼:
print("This program prints your invoices."
"\nPlease enter the item identification, item cost and quantity sold when promted."
"\nEnter 'done' when no more items"
"\n=========================================")
saveqty= ()
savetprice=()
qtysum= 0 #quantity =qty for short
sumprice=0
list1 = []
totalprice=0
while True:
itemid = input('Item identification: ')
if itemid == "done":
break
if len(itemid)<3:
print("item identification should be at least 3 characters long, try again")
continue
else:
list11 = list[itemid]
list1 +=[itemid]
qtysold = input("Qty sold: ")
try:
qtysold =int(qtysold)
except ValueError:
print("must be an integer value, try again")
continue
qtysum+=qtysold
try:
itemprice = float(input("Item price: "))
savetprice= (itemprice)
except ValueError:
print("item price must be numerical value, try again")
continue
totalprices= (qtysold*itemprice)
totalprice+=totalprices
for elem in list1:
print(qtysold,'x ',elem, '@ ', savetprice, 'SAR', '===', totalprices)
total = sumprice
itemtotal = qtysum
print("=========================================\nNo. of items purchased: ", itemtotal,"\nTotal price is: ", totalprice, "SAR")
以下是解決您的問題的代碼
print("This program prints your invoices."
"\nPlease enter the item identification, item cost and quantity sold when promted."
"\nEnter 'done' when no more items"
"\n=========================================")
saveqty = ()
savetprice = ()
qtysum = 0 # quantity =qty for short
sumprice = 0
list1 = []
totalprice = 0
while True:
itemid = input('Item identification: ')
if itemid == "done":
break
if len(itemid) < 3:
print("item identification should be at least 3 characters long, try again")
continue
qtysold = input("Qty sold: ")
try:
qtysold = int(qtysold)
except ValueError:
print("must be an integer value, try again")
continue
qtysum += qtysold
try:
itemprice = float(input("Item price: "))
savetprice = (itemprice)
except ValueError:
print("item price must be numerical value, try again")
continue
totalprices = (qtysold * itemprice)
totalprice += totalprices
list1.append((itemid, qtysold, savetprice, totalprices))
for elem, qtysold, savetprice, totalprices in list1:
print(qtysold, 'x ', elem, '@ ', savetprice, 'SAR', '===', totalprices)
total = sumprice
itemtotal = qtysum
print("=========================================\nNo. of items purchased: ", itemtotal, "\nTotal price is: ", totalprice, "SAR")
Output:
This program prints your invoices.
Please enter the item identification, item cost and quantity sold when promted.
Enter 'done' when no more items
=========================================
Item identification: 123
Qty sold: 5
Item price: 20
Item identification: 456
Qty sold: 3
Item price: 30
Item identification: done
5 x 123 @ 20.0 SAR === 100.0
3 x 456 @ 30.0 SAR === 90.0
=========================================
No. of items purchased: 8
Total price is: 190.0 SAR
注意:如果您想稍后打印出來,您需要將while
循環中的所有信息(例如itemid
、 qtysold
)保存到list1
中。 否則,退出while
循環時, qtysold
和totalprices
將始終保留最后一個值。 這解釋了您所面臨問題的原因。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.