首页 > 解决方案 > python中的发票(收据)程序。如何防止覆盖旧值

问题描述

我是 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")

标签: pythoncsspython-3.xwhile-loop

解决方案


以下是解决您的问题的代码

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

注意:您需要保存循环中的所有信息(例如itemid,,qtysold),以便以后打印它们。否则,退出循环时将始终保留最后一个值。这解释了您所面临问题的原因。whilelist1qtysoldtotalpriceswhile


推荐阅读