Stack Overflow Asked on January 5, 2022
I am currently working on a piece of code that returns a user’s requested toppings on a pizza.
Here is the code:
def topping_menu():
print("""
What toppings do you want on your pizza?
_____________________________________________________
| 1:Bacon | 5:Anchovies | 9:Black Olives |
| 2:Pepperoni | 6:Spinach | 10:Chicken |
| 3:Mushrooms | 7:Onion | 11:Ground Beef |
| 4:Pineapple | 8:Bell Peppers | 12:Jalapenos |
|________________|__________________|_________________|
What toppings do you want on your pizza?
""")
def topping_order():
topping_mappings = {
1: 'Bacon',
2: 'Pepperoni',
3: 'Mushrooms',
4: 'Pineapple',
5: 'Anchovies',
6: 'Spinach',
7: 'Onions',
8: 'Bell Peppers',
9: 'Black Olives',
10: 'Chicken',
11: 'Ground Beef',
12: 'Jalapenos''
}
requested_toppings = []
while True:
response = input('-')
toppings_wanted = response
toppings_wanted = topping_mappings[toppings_wanted]
requested_toppings.append(requested_toppings)
if response == 'q':
break
for requested_topping in requested_toppings:
if requested_topping in topping_mappings:
print(f"Adding: {toppings_wanted}")
else:
print(f"We do not have {requested_topping}")
topping_total = len(requested_toppings) * float(1.23)
print("nWe are adding the requested toppings to your pizza.")
print(f"your topping total will be: ${topping_total}")
topping_menu()
topping_order()
However when I run the code I get the following error:
toppings_wanted = topping_mappings[toppings_wanted]
KeyError: '1'
The code should have returned when user enters ‘1’:
Adding: Bacon
-q
We are adding the requested toppings to your pizza.
your topping total will be: $1.23
My guess is the issue has to do something with ‘topping_mappings’, or possibly the while loop because this was the last thing I added before I received this error. I have spent some time researching this issue however I have come up with nothing that is helpful.
My question:
Why do I keep getting this issue and how can I fix it so I do not make this error again.
Thank you in advance.
Here I edited and added comments where you were wrong.
`def topping_menu(): print(""" What toppings do you want on your pizza?
_____________________________________________________
| 1:Bacon | 5:Anchovies | 9:Black Olives |
| 2:Pepperoni | 6:Spinach | 10:Chicken |
| 3:Mushrooms | 7:Onion | 11:Ground Beef |
| 4:Pineapple | 8:Bell Peppers | 12:Jalapenos |
|________________|__________________|_________________|
What toppings do you want on your pizza?
""")
def topping_order():
topping_mappings = {
1: 'Bacon',
2: 'Pepperoni',
3: 'Mushrooms',
4: 'Pineapple',
5: 'Anchovies',
6: 'Spinach',
7: 'Onions',
8: 'Bell Peppers',
9: 'Black Olives',
10: 'Chicken',
11: 'Ground Beef',
12: 'Jalapenos'
}
requested_toppings = []
while True:
response = input('-')
if response == 'q':#Checking for Quit
break
toppings_wanted = response
toppings_wanted = topping_mappings[int(toppings_wanted)]#Converting response to int
requested_toppings.append(toppings_wanted)
#for requested_topping in requested_toppings:
#You dont need the for loop
if toppings_wanted in topping_mappings.values():#You were not comparing it with Values
print(f"Adding: {toppings_wanted}") #This should be toppings wanted
else:
print(f"We do not have {toppings_wanted}") #This also should be toppings wanted
topping_total = len(requested_toppings) * float(1.23) #You dont have to write float python knows its float by default
print("nWe are adding the requested toppings to your pizza.")
print(f"your topping total will be: ${topping_total}")
`
Answered by Sunny on January 5, 2022
this is wrong
requested_toppings.append(requested_toppings)
it should be
requested_toppings.append(toppings_wanted)
and you need to take int as an input
response = int(input('-'))
and u have an extra '
12: 'Jalapenos''
Answered by Sunny on January 5, 2022
response = input('-')
the input
function here will return str
.
Your dict topping_mappings
here has key as int
. So you need to convert to int
before this toppings_wanted = topping_mappings[toppings_wanted]
the following change is one way to fix it.
response = input('-')
toppings_wanted = int(response) if response.isdigit() else None
toppings_wanted = topping_mappings[toppings_wanted]
Answered by Poojan on January 5, 2022
The issue is toppings_wanted is a string and your keys are ints
toppings_wanted = topping_mappings[int(toppings_wanted)]
Answered by Harben on January 5, 2022
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP