python - 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")
解决方案
以下是解决您的问题的代码
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
),以便以后打印它们。否则,退出循环时将始终保留最后一个值。这解释了您所面临问题的原因。while
list1
qtysold
totalprices
while
推荐阅读
- c - Why is my program not displaying any output?
- html - Is it possible to have an unordered list with bullets inside and hanging indent?
- swift - MapView viewForAnnotation 在圆圈中显示白色针脚(默认)
- html - 自动以 html 格式显示 youtube 的最新 5 个视频
- konvajs - 关闭 Konvajs 警告
- gradle - 无法将发布“mavenJava”发布到存储库“maven”
- php - PHP class_exists() 找不到真正存在的类
- asp.net-mvc - 指定实现时带有接口类的 WebAPI 模型 [ModelBinder]
- python - How to get ckeditor field value in modelform after submit in Django2.1?
- ms-access - 连接 Power BI 以通过网络访问数据库