Matching Dict Keys With User Input In Python
I am creating a python pizza app (yes I know there is a ton of them) but, I am coming across a scenario that I need help understanding. price = 0 toppings = { 'mushrooms': .5,
Solution 1:
You can do it multiple ways:
- Use if
order1 in pizzaSize
, if it is true, add the price - Use
.get()
to see if the size is present in the dictionary and react accordingly. - Use
pizzaSize[order1]
and catch the exception if the key is not present in dict.
Solution 2:
You need may use an .get()
to get the corresponding value from the dict, if it doesn't exist you'll a None
order1 = input("Welcome to the pizza store what size pizza can we get for you (small, medium, large, or x-large): " )
size_price = pizzaSize.get(order1)
if size_price is not None:
price += size_price
else:
print(f"Sorry the size '{order1}' doesn't exist")
exit(1)
Post a Comment for "Matching Dict Keys With User Input In Python"