2

I am not particularly a beginner on Python but i'm not too experienced either.

I am working on a small project and am wondering what is the best course of action. I want to make a food dictionary of different frozen foods that display the nutritional value of each entry.

An example for an entry in Food_Catalog_Frozen.py would be:

Burrito = {
    "calories": 310,
    "fats": 14,
    "sodium": 330,
    "carbohydrates": 35,
    "sugars": 1,
    "protein": 9,
}

Then I would like to make a separate "What's cookin'?" script where a user inputs everything they ate and all the values would be added.

Before dealing with any analytics I wanted to see if I could print out the foods as they are. The following is the code to try print out the nutritional information for the burrito above.

import Food_Catalog_Frozen as Frozen

print("What's cookin', good lookin'?\n")

STOP = False

while STOP == False:
    food = input()  # Enter Burrito
    if food == "done":
        STOP = True
    else:
        print(Frozen.food)

And the following error is thrown:

 print(Frozen.food)
AttributeError: module 'Food_Catalog_Frozen' has no attribute 'food'

The major problem is the variable food, although in this instance now valued as Burrito, the dictionary is still searching for Frozen.food instead of Frozen.Burrito which does work. It appears that it is not possible to search dictionaries through user input.

If anyone could say why this is not working or if there is a better way to do this that would be greatly appreciated. Thank you.

azro
  • 53,056
  • 7
  • 34
  • 70
Plums
  • 25
  • 3
  • Can you show all your code for Food_Catalog_Frozen.py? – TYZ May 10 '20 at 13:13
  • Where do you see a `food` attribute in Food_Catalog_Frozen ? Do `print(Frozen.Burrito)` – azro May 10 '20 at 13:14
  • The Burrito lines are all the entries I have in so far. That's all the code for now. I plan on adding more once I can get it to work. – Plums May 10 '20 at 13:15
  • Does this answer your question? [Why dict.get(key) instead of dict\[key\]?](https://stackoverflow.com/questions/11041405/why-dict-getkey-instead-of-dictkey) – azro May 10 '20 at 13:15
  • You need to `Frozen.Burrito[food]` – azro May 10 '20 at 13:15
  • food is only a variable name that stores in what a user has inputted. It is not present anywhere on the Food_Catalog_Frozen.py. So if a user inputs Burrito I would like the result to be interpreted as Frozen.Burrito which is present on Food_Catalog_Frozen.py. – Plums May 10 '20 at 13:25

2 Answers2

1

Quick solution is to do print(getattr(Frozen, food)). This would work because Burrito is an actual variable (attribute) of Frozen, and getattr is used to fetch attributes. However, I wouldn't recommend this solution.

Best route here would be to rethink your design. Instead of having individual variables corresponding to the possible types of foods, you can use a top-level dict:

foods = {
    "Burrito": {
        "calories": 310,
        "fats": 14,
        "sodium": 330,
        "carbohydrates": 35,
        "sugars": 1,
        "protein": 9
    },
    ...
}

Then you would change print(Frozen.food) to print(Frozen.foods[food]). If you wanted to make sure the entered food is registered, you could wrap the print inside the following if:

if food in Frozen.foods:
    print(Frozen.foods[food])
Mario Ishac
  • 5,060
  • 3
  • 21
  • 52
1

When you search for Frozen.food, you're literally checking for an entry called "food" in the frozen food catalog.

Note that this is independent of the input variable food. No matter what value food holds ("Burrito" in your example), the lookup is made for Frozen.food which doesn't exist.

What you can do instead is to use a top level dict as the other answer has suggested.

Another solution is to define a function, say get_food(food_name) in the catalog.

You can then call get_food(food) in your main/client class and get the required dict as output

uggi121
  • 11
  • 1