我在创建此程序时遇到问题我不知道是否应该使用elif或其他内容。
以下是问题:在下面的单元格中,使用try / except控制结构创建一个在字典中查找价格的程序。
shop_prices = {
'eggs': 1.99,
'milk': 0.99,
'ham': 4.99,
}
# take two inputs - what the customer wants, and how many of the items they want
# always greet the customer
# see if they sell the item and calculate the price
# otherwise say "We don't sell XXX", where XXX is the item
# always say goodbye to the customer
答案 0 :(得分:1)
这可能是您正在寻找的。它询问你想要什么,如果它不可用,它会再次询问。之后,它会询问您需要多少项目,如果该输入有效,则会打印出成本并退出。
shop_prices = { 'eggs': 1.99, 'milk': 0.99, 'ham': 4.99, }
request = input("Hello, what would you like?\n")
while request not in shop_prices.keys():
request = input("That item isn't currently available, please choose another item.\n")
while True:
try:
numof = int(input("How many of that item would you like?\n"))
break
except ValueError:
print("That isn't an integer, please enter an integer.\n")
print("That will be $"+str(numof*shop_prices[request])+". Thank you for shopping here today.\n")