我是一个新的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")
输出:
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
将始终保留最后一个值。这就解释了你所面临的问题的原因。