您好我是python编程的新手。我刚开始学习python。我需要一些关于如何构建以下程序的想法/部分代码。任何想法都表示赞赏。我必须得到以下输出。
----- Welcome to my checkout! -----
Please enter the barcode of your item: 123
Milk, 2 Litres - $2.0
Would you like to scan another product? (Y/N) y
Please enter the barcode of your item: 456
Bread - $3.5
Would you like to scan another product? (Y/N) y
Please enter the barcode of your item: 999
This product does not exist in our inventory.
Would you like to scan another product? (Y/N) n
Payment due: $5.5. Please enter an amount to pay: 5
Payment due: $0.5. Please enter an amount to pay: -3
We don't accept negative money!
Payment due: $0.5. Please enter an amount to pay: 2
----- Final Receipt -----
Milk, 2 Litres $2.0
Bread $3.5
Total amount due: $5.5
Amount received: $7.0
Change given: $1.5
Thank you for shopping at mytsore!
(N)ext customer, or (Q)uit? q
我到现在所做的是:
items = {'banana': {'price': 4, 'stock': 6, 'code':123 },
'apple': {'price': 2, 'stock': 0,'code':1231 },
'orange': {'price': 1.5, 'stock': 32,'code':1233},
'pear': {'price': 3, 'stock': 15,'code':12335},
}
bar=int(input("enter barcode:"))
sum = 0
price =[]
products =[]
for key in items:
if items[key]['code'] == int(bar):
print("item found")
print (key)
print ("price: %s" % items[key]['price'])
print ("stock: %s" % items[key]['stock'])
print ("Barcode: %s" % items[key]['code'])
price.append(items[key]['price'])
products.append(key)
print(price)
print(products)
print("..............................................")
while True:
choice = input("scan another item?y/n")
if choice =='n':
for cost in price:
sum += cost
print("Total cost:",sum)
print("..............................................")
if choice =='y':
bar=int(input("enter barcode:"))
for key in items:
if items[key]['code'] == int(bar):
print("item found")
print (key)
print ("price: %s" % items[key]['price'])
print ("stock: %s" % items[key]['stock'])
print ("Barcode: %s" % items[key]['code'])
price.append(items[key]['price'])
products.append(key)
#print(price)
如果用户选择'n'程序应该停止并生成带有价格的账单/收据。即使我按'n'选项,我仍然会收到消息“扫描另一个项目?y / n”)“。如何构建我的代码以获得低于预期的输出?我希望将我的代码包装在函数内并使用OOP概念创建实例
答案 0 :(得分:0)
缺少中断
while True:
choice = input("scan another item?y/n")
if choice =='n':
for cost in price:
sum += cost
print("Total cost:",sum)
print("..............................................")
break
答案 1 :(得分:0)
items = {'banana':{'price': 4, 'stock': 6, 'code':123 },
'apple':{'price': 2, 'stock': 0,'code':1231 },
'orange':{'price': 1.5, 'stock': 32,'code':1233},
'pear':{'price': 3, 'stock': 15,'code':12335},}
bar=int(input("enter barcode:"))
sum = 0
price =[]
products =[]
for key in items:
if items[key]['code'] == int(bar):
print("item found")
print (key)
print ("price: %s" % items[key]['price'])
print ("stock: %s" % items[key]['stock'])
print ("Barcode: %s" % items[key]['code'])
price.append(items[key]['price'])
products.append(key)
print(price)
print(products)
print("..............................................")
while True:
choice = input("scan another item?y/n")
if choice =='y':
bar=int(input("enter barcode:"))
for key in items:
if items[key]['code'] == int(bar):
print("item found")
print (key)
print ("price: %s" % items[key]['price'])
print ("stock: %s" % items[key]['stock'])
print ("Barcode: %s" % items[key]['code'])
price.append(items[key]['price'])
products.append(key)
#sum += items[key]['çost']
#print(price)
if choice =='n':
for cost in price:
sum += cost
break
print("Total cost:",sum)
print("..............................................")
break