jeudi 23 avril 2020

How to random choice from a certain dictionary that was the user's input

I've been trying to create a program that tells me what to eat on a number of criteria. First it asks me whether preparation time should be low, average or high. It then asks me what type of food I want. It can also give me a type of meat to eat if I only want to know that.

import random
    pasta = {"tonijn": 0, "carbonara": 1, 'pittige': 0}
    speciaal = {'sucuk broden': 2, 'wraps': 1, 'ovenschotel': 2, 'bulgur met kip/gehakt': 1, 'bruschettas': 2,
                'zelf pizza': 2, 'kip tomaat ovenschotel': 2}
    fancy = {'biefstuk': 1, 'pizza bestellen': 0, 'doner bestellen': 0, "uiteten": 1}
    pittig = {'pittige pasta': 0}
    gezond = {'aardappel kip salade': 1, 'kipfilet, groente en bulgur': 0}
    ongezond = {"soep en pannenkoeken": 0, 'shoarma': 1}
    basis = {'avg': 1, "pasta": 0, 'curry': 1}
    vlees = ['schnitzel', 'varkenslapje', 'hoouthakkersburger', 'cordon bleu', 'kip krokant']

tijd = str(input("Prep time? Short, medium or long ")).lower()
print('Options:')
print('Pasta - Speciaal - Fancy - Pittig - Gezond - Ongezond - Basis - Welk vlees')
type_eten = str(input("Your choice: ")).lower()

if type_eten == 'welk vlees':
    print('The meat ur gonna eat: ', random.choice(vlees))
elif tijd == "short":
    for food, value in type_eten:
        mogelijke_opties = []
        if value < 1:
            mogelijke_opties.append(eten)
            antwoord = random.choice(mogelijke_opties)
            print("What ur gonna eat is: ", antwoord)
elif tijd == "medium":
    for food, value in type_eten:
        mogelijke_opties = []
        if waarde < 2:
            mogelijke_opties.append(eten)
            antwoord = random.choice(mogelijke_opties)
            print("Wat je gaat eten is: ", antwoord)
else:
    print('What ur gonna eat is: ', random.choice(list(type_eten)))

When I try to run this, it gives me the error:

AttributeError: 'str' object has no attribute 'items'

for the code:

for food, value in type_eten.items():

I first created a dictionary in which the short prep time meals have a 0 as a value, medium 1 and long 2. It then asks the user what he/she wants. If they only want to know what meat they're gonna eat, it'll give them that. Otherwise it checks the prep time and creates a new list with the types of food that are in a certain dictionary with the corresponding prep time. I then want it to choose something from that new list. To do that, I think I should be able to add at .items() after 'type_eten', so it would look like this;

elif tijd == "short":
        for food, value in type_eten.items():
            mogelijke_opties = []
            if value < 1:
                mogelijke_opties.append(eten)
                antwoord = random.choice(mogelijke_opties)
                print("What ur gonna eat is: ", antwoord)

I don't know whether that would solve the problem but after some research on stackoverflow, I think it should. type_eten doesn't give me the possibility however and I'd have to add all the dictionary names which would make the code a lot longer. Is there another way to do this?




Aucun commentaire:

Enregistrer un commentaire