while statements

Zentrader zentraders at gmail.com
Wed Oct 17 00:33:57 EDT 2007


O> > while cal <=0:
> >    #Prompt for calories
> >        cal = input("Please enter the number of calories in your food: ")
> >        if cal <=0:
> >            print "Error.  The number of calories must be positive."
>
> >    #Prompt for fat
> >        fat = input("Please enter the number of fat grams in your food: ")
> >        if fat <=0:
> >            print "Error.  The number of fat grams must be positive."

You can also use a function
def get_value( lit ):
   ret_val = 0
   while ret_val <= 0:
      ret_val  = input("Please enter the number of %s in your food: "
% (lit))
      if ret_val <=0:
         print "Error.  The number of %s must be positive." % (lit)
   return ret_val
#
cal = get_value( "calories" )
fat = get_value( "fat grams" )
##
## Calculate values, etc.




More information about the Python-list mailing list