允许用户输入任何数字的程序,如果该数字是预制字典的一部分,则会询问用户该项目的数量,然后输出收据:
12345670 apple 2 2.00
total cost of orders 2.00
我的字典将是
items = {'12345670' : {'name' : 'apple', 'price' : 1},
'87654325' : {'name' : 'orange', 'price' : 1}}
print("Hi There! Welcome to sSpecialists!")
print("To start shopping, note down what you want to buy and how much of it")
print("Here are the purchasable items")
print("~~~~~")
print("12345670 is a hammer (£4.50)")
print("87654325 is a screw driver (£4.20)")
print("96385272 is a pack of 5 iron screws (£1.20)")
print("74185290 is pack of 20 100mm bolts (£1.99)")
print("85296374 is a pack of 6 walkers crisps (£1)")
print("85274198 is haribo pack (£1)")
print("78945616 is milk (£0.88)")
print("13246570 is a bottle of evian water (£0.99)")
print("31264570 is kitkat original (£0.50)")
print("91537843 is a cadbury bar (£1)")
print('88198101 is sausage, price is(£6.75)')
print('88198118 is stuffed shells, price is(£7.75)')
print('88198125 is manicotti, price is(£7.75)')
print('88198132 is chicken & broccoli pasta, price is(£11.00)')
print('88198149 is baked ravioli, price is(£5.00)')
print('88198156 is ribs & hand-breaded shrimp, price is(£8.40)')
print('88198163 is hickory bourbon-glazed pork chop, price is(£9.99)')
print('88198170 is grilled chicken & crab cake combo, price is(£12.75)')
print('88198187 is double decker chicken, price is(£7.99)')
print('88198194 is grilled salmon, price is(£10.00)')
print('88198200 is herb-crusted tilapia , price is(£4.00)')
print("~~~~~")
possibleOrders = {'12345670' : {'name' : 'hammer', 'price' : 4.50},
'87654325' : {'name' : 'screwDriver', 'price' : 4.20},
'96385272' : {'name' : 'packOf5IronnScrews', 'price' : 1.20},
'74185290' : {'name' : 'packOf200mmBolts', 'price' : 1.99},
'85296374' : {'name' : 'packOf6WalkersCrisps', 'price' : 1},
'85274198' : {'name' : 'hariboPack', 'price' : 1},
'78945616' : {'name' : 'milk', 'price' : 0.88},
'13246570' : {'name' : 'bottleOfEvianWater', 'price' : 0.99},
'31264570' : {'name' : 'kitkatOriginal', 'price' : 0.50},
'88198101' : {'name' : 'sausage', 'price' : 6.75},
'88198118' : {'name' : 'stuffedShells', 'price' : 7.75},
'88198125' : {'name' : 'manicotti', 'price' : 7.75},
'88198132' : {'name' : 'chicken&broccoliPasta', 'price' : 11.00},
'88198149' : {'name' : 'baked_ravioli', 'price' : 5.00},
'88198156' : {'name' : 'ribs&handBreadedShrimp', 'price' : 8.40},
'88198163' : {'name' : 'hickoryBourbonGlazedPorkChop', 'price' : 9.99},
'88198170' : {'name' : 'grilledChicken&crabCakeCombo', 'price' : 12.75},
'88198187' : {'name' : 'doubleDeckerChicken', 'price' : 7.99},
'88198200' : {'name' : 'herbCrustedTilapia', 'price' : 4.00},
'91537843' : {'name' : 'cadburyBar', 'price' : 1}}
print("Alright, now start typing what you want to order")
print(" ")
price = 0
full_list = " "
chos_items = []
while full_list != "":
print(" ")
full_list = input("Type: ")
if full_list == 'end':
break
item = int(full_list)
amount = int(input("Amount: "))
item = int(full_list)
if full_list in possibleOrders:
orders = print("{} {:>5} (£{:0.2f})".format(full_list, possibleOrders[full_list]['name'], possibleOrders[full_list]['price']))
if orders != "":
chos_items.append(full_list)
price = int(amount) * (possibleOrders[full_list]['price']) + price
print("Subtotal is currently at "+str(price))
print("Your subtotal is: " +str(price))
receipt = input("Would you like to view your receipt?: ")
if receipt == 'yes':
print(chos_items)
这是我的代码,我需要保存用户输入的内容,并将其翻译成我预先制作的dictinary对应的数字
答案 0 :(得分:0)
要检查用户条目是否在字典中,您可以if entry in items.keys()
或items.get(entry)
。第一个将检查字典中是否有该条目。然后,获得该密钥的值是微不足道的。如果它是dict,第二个将返回该键的值,否则它将不返回任何值。
建议的解决方案:
available_products = {
'1234': {
'name': 'hammer',
'price': 4.50
},
'4321': {
'name': 'screw driver',
'price' : 4.20
}
}
print("----------------------------------------")
for key, product in available_products.items():
print("{key} - {name} ${price}".format(key=key, name=product['name'], price=product['price']))
print("----------------------------------------")
print("Alright, now start typing what you want to order. \n")
list_of_products = []
subtotal = 0
while(True):
entry = input('Type: ')
if entry == 'end':
break
product = available_products.get(entry)
if product:
print('{key} {name} ${price}'.format(key=entry, name=product['name'], price=product['price']))
amount = int(input('How many? '))
subtotal += amount * product['price']
list_of_products.append((entry, amount))
print('Subtotal is {}'.format(subtotal))
else:
print("Product is not available")
print('Your receipt is:')
for entry, amount in list_of_products:
product = available_products.get(entry)
print("{key} - {amount}x {name} - ${price}".format(key=entry, amount=amount, name=product['name'], price=product['price']))
print("Total price is {} \n".format(subtotal))