在字典中查找价格的程序

program to look up price in dictionary

我在创建这个程序时遇到问题我不知道我应该使用 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

这可能就是您要找的。它会询问您想要什么,如果没有,它会再次询问。之后,它会询问您想要多少该商品,如果该输入有效,它会打印出成本并退出。

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