[Tutor] Need help with dict.calculation

Andy W toodles@yifan.net
Thu, 24 Jan 2002 14:38:10 +0800


> 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

Sorry, I thought you had total_calories defined somewhere else. Danny has
already shown the solution to this though.
(Look down for the running total approach)

>
> > #
> >
> > 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.

Do you intend to use the contents of the dictionary after you've calculated
the total calories? If not, it isn't really necessary to ask what type of
food it is, nor even to keep the data in a dictionary. This is all you would
need:

def gather_data():
  portion=input("Enter number of portions: ")
  cal_portion=input("Enter calories per portion: ")
  return portion,cal_portion

def get_total_calories():
  print "Enter all the foods you have eaten today."
  total_calories=0        #It's now an integer instead of a dictionary, just
to hold the number of calories
  more=""
  while more!="n":
    portion,cal_portion=gather_data()
    total_calories=total_calories+portion*cal_portion
    more=raw_input("Do you want to continue? y/n: ")
  print "Total calories: ",total_calories

But if you need to know what the names of the foods and such things
afterwards... stick to your first approach.

HTH, Andy

>
> 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
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>