0

Because programming is one of my favorite hobbies I started a small project in python.

I'm trying to make a nutritional calculator for daily routine, see the code below:

# Name: nutri.py 
# Author: pyn

my_dict = {'chicken':(40, 50, 10),
        'pork':(50, 30, 20)
         }

foods = raw_input("Enter your food: ")

#explode / split the user input

foods_list = foods.split(',')

#returns a list separated by comma

print foods_list

What I want to do:

  1. Get user input and store it in a variable
  2. Search the dictionary based on user input and return the asociated values if the keys / foods exists
  3. Sum these values in different nutritional chunks and return them, something like: You ate x protein, y carbs and z fat etc.

Any ideas are welcome.

3 Answers 3

0
my_dict = {'chicken':(40, 50, 10),
        'beef':(50, 30, 20)
         }

foods = raw_input("Enter your food: ")

#explode / split the user input

foods_list = foods.split(',')

#returns a list separated by comma

#print foods_list

nuts = [0, 0, 0]

for food in foods_list :
    if food.strip() in my_dict: 
        i = 0
        for value in  my_dict[food.strip()]:
            nuts[i] += value        
            i += 1  
print nuts  

scripts$ python nutrition.py
Enter your food: chicken, pork, beef
[90, 80, 30]


some improvements ;)
2
  • dude this won't work when the user will enter an incorrect input(food). I have posted one below this. U may check out that it will work for any case.
    – shalki
    Feb 7, 2013 at 11:27
  • yours would not work if the user enters chicken,[space] pork :P
    – Redian
    Feb 7, 2013 at 11:38
0

Here is my solution, It checks if a food is in the dictionary and will state if its not.

my_dict = {'chicken':(40, 50, 10),
        'pork':(50, 30, 20)
         }

foods = raw_input("Enter your food: ")

foods_list = foods.split(',')

empty_list = []
for food in foods_list:
    if food in my_dict:
        empty_list.append(list(my_dict[food]))
    else:
        print '%s has no nutritional information and will not be included in the calculation' % food

values = [sum(x) for x in zip(*empty_list)]

print 'Total protein = %d, Total Carbs = %d, Total Fat = %d' % (values[0],values[1],values[2])

This outputs:

Enter your food: chicken,pork,pizza
pizza has no nutritional information and will not be included in the calculation
Total protein = 90, Total Carbs = 80, Total Fat = 30
0

This code will do all the necessary stuff u are trying to do:

my_dict = {'chicken':(40, 50, 10),
        'pork':(50, 30, 20)
         }

foods = raw_input("Enter your food: ")

#explode / split the user input

foods_list = foods.split(',')

#returns a list separated by comma

t=[0,0,0]

print foods_list

for i in foods_list:
    if i.strip() in my_dict:
        v=my_dict.get(i.strip())
        t[0]=t[0]+v[0]
        t[1]=t[1]+v[1]
        t[2]=t[2]+v[2]

print t

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.