【问题标题】:invoice (receipt) program in python. How to prevent overwriting old valuespython中的发票(收据)程序。如何防止覆盖旧值
【发布时间】:2021-05-29 21:30:08
【问题描述】:

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

【问题讨论】:

  • 快速提问,list11 = list[itemid] 这一行发生了什么?
  • 嗨@jidicula,不知道哈哈。在我关于列表的课程中,其中一个示例使用了类似的代码。我用它来实现我的代码。但它不需要对吗?我想将 itemid 转换为列表。
  • 我更担心的是它似乎是一个错字,因为list 本身就是一个type 对象,而type 对象是不可下标的。
  • 如果您想将itemid 转换为列表以连接到list1,您可以用方括号将其括起来:[itemid]。或者,您可以使用list1.append(itemid) 附加到list1
  • 我刚刚尝试使用您的建议,并且有效。我现在更好地理解它了。非常感谢您的帮助

标签: python css python-3.x while-loop


【解决方案1】:

下面是解决问题的代码

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 循环中的所有信息(例如,itemidqtysold)保存到 list1。否则,qtysoldtotalprices 在退出 while 循环时将始终保留最后一个值。这解释了您所面临问题的原因。

【讨论】:

  • 非常感谢。我非常感谢您的帮助。现在说得通了
猜你喜欢
  • 2013-12-26
  • 2011-06-18
  • 1970-01-01
  • 2015-01-10
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
  • 2010-10-26
  • 2017-12-19
相关资源
最近更新 更多