while statements

S

Shawn Minisall

I just learned about while statements and get why you place them around
inputs for validation, but I'm a little lost on exactly where to place
it with what condition in this program where the number of fat grams
exceeds the total number of calories so that it loops back and asks you
the two questions again instead of just saying The calories or fat grams
were incorrectly entered. Any idea's?

thx

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


#Calculate calories from fat
calfat = float(fat) * 9

#Calculate number of calories from fat
caldel = calfat / cal

#change calcent decimal to percentage
calcent = caldel * 100

#evaluate input
if calfat > cal:
print "The calories or fat grams were incorrectly entered."

elif calcent > 0 and calfat < cal:

if caldel <= .3:
print "Your food is low in fat."
elif caldel >= .3:
print "Your food is high in fat."


#Display percentage of calories from fat
print "The percentage of calories from fat in your food is %",
calcent
 
D

danfolkes

I just learned about while statements and get why you place them around
inputs for validation, but I'm a little lost on exactly where to place
it with what condition in this program where the number of fat grams
exceeds the total number of calories so that it loops back and asks you
the two questions again instead of just saying The calories or fat grams
were incorrectly entered. Any idea's?

thx

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

#Calculate calories from fat
calfat = float(fat) * 9

#Calculate number of calories from fat
caldel = calfat / cal

#change calcent decimal to percentage
calcent = caldel * 100

#evaluate input
if calfat > cal:
print "The calories or fat grams were incorrectly entered."

elif calcent > 0 and calfat < cal:

if caldel <= .3:
print "Your food is low in fat."
elif caldel >= .3:
print "Your food is high in fat."

#Display percentage of calories from fat
print "The percentage of calories from fat in your food is %",
calcent

Instead of: if(cal<=0)

you could do :
cal=0
while cal<=0:
cal = int(raw_input("Please enter the number of calories in your
food: "))

that would make sure that your input is > 0
 
F

Francesco Guerrieri

Instead of: if(cal<=0)

you could do :
cal=0
while cal<=0:
cal = int(raw_input("Please enter the number of calories in your
food: "))

that would make sure that your input is > 0

Calories could be non integer :)

francesco
 
K

kyosohma

I just learned about while statements and get why you place them around
inputs for validation, but I'm a little lost on exactly where to place
it with what condition in this program where the number of fat grams
exceeds the total number of calories so that it loops back and asks you
the two questions again instead of just saying The calories or fat grams
were incorrectly entered. Any idea's?

thx

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

#Calculate calories from fat
calfat = float(fat) * 9

#Calculate number of calories from fat
caldel = calfat / cal

#change calcent decimal to percentage
calcent = caldel * 100

#evaluate input
if calfat > cal:
print "The calories or fat grams were incorrectly entered."

elif calcent > 0 and calfat < cal:

if caldel <= .3:
print "Your food is low in fat."
elif caldel >= .3:
print "Your food is high in fat."

#Display percentage of calories from fat
print "The percentage of calories from fat in your food is %",
calcent

I would think using 2 while loops would be easiest.

<code>
# untested

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

I'll leave the rest up to you.

Mike
 
M

martyw

Shawn said:
I just learned about while statements and get why you place them around
inputs for validation, but I'm a little lost on exactly where to place
it with what condition in this program where the number of fat grams
exceeds the total number of calories so that it loops back and asks you
the two questions again instead of just saying The calories or fat grams
were incorrectly entered. Any idea's?

thx

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


#Calculate calories from fat
calfat = float(fat) * 9
#Calculate number of calories from fat
caldel = calfat / cal

#change calcent decimal to percentage
calcent = caldel * 100

#evaluate input
if calfat > cal:
print "The calories or fat grams were incorrectly entered."

elif calcent > 0 and calfat < cal:
if caldel <= .3:
print "Your food is low in fat."
elif caldel >= .3:
print "Your food is high in fat."

#Display percentage of calories from fat
print "The percentage of calories from fat in your food is %",
calcent
you could change to something like this


while True: # don test in the loop
#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."
continue ### here you don need to go any further in this loop

#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."
continue ### here you don need to go any further in this loop


#Calculate calories from fat
calfat = float(fat) * 9
#Calculate number of calories from fat
caldel = calfat / cal

#change calcent decimal to percentage
calcent = caldel * 100

#evaluate input
if calfat > cal:
print "The calories or fat grams were incorrectly entered."

#elif calcent > 0 and calfat < cal:
else: # calcent will be bigger than zero now by construction,
# and I guess you dont want to test for equality of
# calfat and cal
if caldel <= .3:
print "Your food is low in fat."
elif caldel >= .3:
print "Your food is high in fat."

#Display percentage of calories from fat
print "The percentage of calories from fat in your food "\
"is %f%%" % calcent
break # we got a satisfactory result, leave this loop
 
Z

Zentrader

O> > while cal <=0:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top