[Tutor] Need help with dict.calculation
Eve Kotyk
e.kotyk@shaw.ca
Thu, 24 Jan 2002 02:07:37 +0000
Hi Andy..and the rest of you.
Thank you for your suggestions.
> #
> def gather_data():
> items = raw_input("Add food item: ")
> portion = input("Enter number of portions: ")
> cal_portion = input("Enter calories per portion: ")
> return items,portion,cal_portion
>
> def get_total_calories():
> print "Enter all foods you have eaten today."
>
> more=""
> while more!="n":
> items,portion,cal_portion = gather_data()
> total_calories[items]=portion * cal_portion
> more = raw_input("Do you want to continue? y/n: ")
>
> for i in total_calories.values():
> total = total + i
> print "Total calories: ",total
This part still doesn't work for me. I get the following error:
Traceback (innermost last):
File "bmi2.py", line 47, in ?
get_total_calories()
File "bmi2.py", line 33, in get_total_calories
total_calories [items] = portion * cal_portion
NameError: total_calories
> #
>
> As a side note, you don't even need to put it in a dictionary, you can just
> have a running total in an integer variable.
> I said I wouldn't make drastic changes though :o)
Andy I appreciate your not making drastic changes because this is a
learning project. But now that I understand dictionaries just a bit
better (and I still haven't figured out how to add up the subtotals for
each item in a total overall). I'd really appreciate a lesson how to
have a running total in an integer variable.
E
>
> Andy
>
> >
> > def food_add():
> > print "Enter all foods you have eaten today."
> >
> > items = raw_input("Add Food Item: ")
> > portion = input("Enter number of portions: ")
> > cal_portion = input("Enter calories per portion: ")
> >
> > more = raw_input("Do you wish to continue? y/n: ")
> > while more != 'n':
> > food_add()
> > break
> > total_calories[items] = portion * cal_portion
> > total = 0
> > for i in total_calories.values():
> > total = total + i
> > print "Total calories: ",total
> >
> > My output is:
> >
> > Total calories: 120
> > Total calories: 254
> >
> > The total calories for each item entered with the last entry first,
> > rather than just a Total calories: 374
> > --
> >
> > ekotyk
> >
> > http://members.shaw.ca/e.kotyk/virtualstudio.htm
> >
> > _______________________________________________
> > Tutor maillist - Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
--
ekotyk
http://members.shaw.ca/e.kotyk/virtualstudio.htm