I am having issues with creating this program I don't know whether I should use elif or something else.
Here is the question: In the cell below, use the try/except control structure to create a program which looks up a price in a dictionary.
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
This may be what you're looking for. It asks what you want, and if it isn't available, it asks again. After that, it asks you how many of that item you want, and if that input is valid, it prints out the cost and exits.
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")