Reputation: 27
I am trying to make a small loop that tells me with my own input whether my topping is available or not, but it's not working correctly. I can only get the message that states: "your choice of topping is not an option" but if I give the word "Topping" as a input it works. Could someone tell me what I am doing wrong?
pizzas = {
'topping' : ['cheese' ,'mozzarella','pepperoni','bacon','basil','garlic','oregano','parmigiano','onion','sausage','tomato','mushroom' ],
}
prompt = "Please pick your topping: "
while True:
search_key = input(prompt)
if search_key == 'quit':
break
elif search_key in pizzas.values():
print("Your topping is availbe")
else:
print("your choice of topping is not an option")
Upvotes: 0
Views: 88
Reputation: 1
Slightly cleaner version would look like this. It means passing input and list from the dictionary to the function that checks if option is in the list.
Your issue was that you were iterating through dictionary keys instead of dictionary values of that key.
pizzas = {
'topping': ['cheese', 'mozzarella', 'pepperoni', 'bacon', 'basil', 'garlic', 'oregano','parmigiano', 'onion', 'sausage', 'tomato', 'mushroom']
}
topping_input_value = input("Please pick your topping: ")
topping_options = pizzas['topping']
def is_option_available(input, key, options):
for option in options:
if input.lower() == option.lower():
print(f"Your {key} is available :)")
return
print(f"Your choice of {key} is not available :(")
# Call out the function, which can be used for all choices made.
is_option_available(topping_input_value, "topping", topping_options)
Upvotes: 0
Reputation: 717
If there's only one key "topping" in dictionary having list of values for toppings then change in code for below one line will be helpful.
elif search_key.lower() in pizzas.get("topping"):
If pizzas.values() is used then it return object of dict_values. To use in operator here i am fetching only value for topping key which returns list of values as object and so i can use in operator with it and it provides desired result.
Also note that user input is converted to lower case while checking within list to make user input case insensitive.
Upvotes: 0
Reputation: 1
You can convert the dict_values to a list with sum(list(pizzas.values()), [])
pizzas = {
'topping' : ['cheese' ,'mozzarella','pepperoni','bacon','basil','garlic','oregano','parmigiano','onion','sausage','tomato','mushroom' ],
}
prompt = "Please pick your topping: "
while True:
search_key = input(prompt)
if search_key == 'quit':
break
elif search_key in sum(list(pizzas.values()), []):
print("Your topping is availbe")
else:
print("your choice of topping is not an option")
Upvotes: -1
Reputation: 39414
You are not searching the topping
list:
pizzas = {
'topping' : ['cheese' ,'mozzarella','pepperoni','bacon','basil','garlic','oregano','parmigiano','onion','sausage','tomato','mushroom' ],
}
prompt = "Please pick your topping: "
while True:
search_key = input(prompt)
if search_key == 'quit':
break
elif search_key in pizzas['topping']:
print("Your topping is available")
else:
print("your choice of topping is not an option")
Upvotes: 1
Reputation:
if search_key in pizzas['topping']
...will return True if the topping (search_key) exists in the list
Upvotes: 1