I'm struggling a bit on a dictionary code in Python (mind I'm a new student without any earlier experience). The strings in this code is on Norwegian, hopefully that's not a problem. The code goes as follow
participants_and_allergens = {"Ole":["gluten", "egg"],
"Silje":["shellfish", "nuts"],
"Ragnar":["milk", "fish"]}
print(participants_and_allergens.keys())
allergens_and_foods = {"Garlic bread": ["gluten", "egg"],
"Fish soup": ["shellfish", "fish"],
"Chocolate Cake": ["milk", "egg"]}
def write_name():
name = input("What is your name?\n> ")#confirms the registration of the participant
if not participants_og_allergener.get(name):
print("Sorry,", name + " does not exist in the registry")
else:
print(name + " is registered and now confirmed")
print(allergens)
print(name + " has the allergens", allergens)
allergens = participants_og_allergener.get(name)
allergens_foods = allergens_and_foods.get("allergens")
print(allergens_foods)
write_name()
Essentially, what I'm asking is, is there any way to connect to dictionaries, which are independent? When the user enters a name, it should result in values linked to the name coming out, and elements from another dictionary.
I have realized I'll have to use a for loop, but I don't know how.
The expected output should be something like "Hi Ole, you have allergies to gluten and eggs and should stay away from the foods garlic bread and chocolate cake"