if then elif

S

Shawn Minisall

I just learned about if, then elif statements and wrote this program.
The problem is, it's displaying all of the possibilities even after you
enter a 0, or if the fat grams are more then the total number of
calories , that is supposed to stop the program instead of continuing on
with the print statements that don't apply. Any idea's? thanks

#Prompt for calories
cal = input("Please enter the number of calories in your food: ")

#Prompt for fat
fat = input("Please enter the number of fat grams in your food: ")

#Input validation
if cal or fat <= 0:
#Display message
print "Error. The number of calories and/or fat grams must be
positive"
print

else:
#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

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

else:
#evaluate input
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

Here's an example of the output...

Please enter the number of calories in your food: 50
Please enter the number of fat grams in your food: 30
Error. The number of calories and/or fat grams must be positive

Your food is low in fat.
The percentage of calories from fat in your food is % 0.0

It was supposed to print The calories or fat grams were incorrectly
entered since the calories from fat was greater then the total number of
calories.
 
B

brad

Shawn said:
I just learned about if, then elif statements and wrote this program.
The problem is, it's displaying all of the possibilities even after you
enter a 0, or if the fat grams are more then the total number of
calories , that is supposed to stop the program instead of continuing on
with the print statements that don't apply. Any idea's? thanks

Two suggestions:

1. Use raw_input instead of input or errors will occur should users
enter non-numeric characters.
#Prompt for calories
cal = input("Please enter the number of calories in your food: ")

#Prompt for fat
fat = input("Please enter the number of fat grams in your food: ")

2. Convert cal and fat to ints or floats for your calculations... maybe
something like this:

try:
int(cal):
except ValueError,e
sys.exit(str(e) + "Enter a number!!!")

This will catch things that cannot be converted to an int (bad user input)

Besides that, I'm pretty sure your calculations are wrong.
 
L

Larry Bates

Shawn said:
I just learned about if, then elif statements and wrote this program.
The problem is, it's displaying all of the possibilities even after you
enter a 0, or if the fat grams are more then the total number of
calories , that is supposed to stop the program instead of continuing on
with the print statements that don't apply. Any idea's? thanks

#Prompt for calories
cal = input("Please enter the number of calories in your food: ")

#Prompt for fat
fat = input("Please enter the number of fat grams in your food: ")

#Input validation
if cal or fat <= 0:
#Display message
print "Error. The number of calories and/or fat grams must be
positive"
print

else:
#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

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

else:
#evaluate input
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

Here's an example of the output...

Please enter the number of calories in your food: 50
Please enter the number of fat grams in your food: 30
Error. The number of calories and/or fat grams must be positive

Your food is low in fat.
The percentage of calories from fat in your food is % 0.0

It was supposed to print The calories or fat grams were incorrectly
entered since the calories from fat was greater then the total number of
calories.

Boolean problem:


if cal or fat <= 0

That may be the way you say it or "think" it but it won't work.

'cal or fat' is evaluated first. Since they both have values this ALWAYS
evaluates to 1 which is NEVER less than or equal to 0.

You are looking for

if (cal <= 0) or (fat <=0):

(Note: Parenthesis not required, but it may help you understand precedence of
evaluation. Also read here:

http://www.ibiblio.org/g2swap/byteofpython/read/operator-precedence.html

-Larry
 
C

chris.monsanto

Boolean problem:

if cal or fat <= 0

That may be the way you say it or "think" it but it won't work.

'cal or fat' is evaluated first. Since they both have values this ALWAYS
evaluates to 1 which is NEVER less than or equal to 0.

You are looking for

if (cal <= 0) or (fat <=0):

(Note: Parenthesis not required, but it may help you understand precedence of
evaluation. Also read here:

http://www.ibiblio.org/g2swap/byteofpython/read/operator-precedence.html

-Larry

that's the most incorrect thing i've heard all day!

if cal or fat <= 0 is parsed as if (cal) or (fat <= 0)
 
T

thebjorn

Boolean problem:

if cal or fat <= 0

That may be the way you say it or "think" it but it won't work.

'cal or fat' is evaluated first. Since they both have values this ALWAYS
evaluates to 1 which is NEVER less than or equal to 0.

That's not correct. The comparison operator has higher presedence than
the or operator, so the above is interpreted as:

if (cal) or (fat <= 0):

This idiom is occasionally useful in a couple of scenarios like e.g.
default and mutable function arguments:

def foo(n=None):
n = n or [42]

which is very common in Perl code, but causes problems if you pass it
an empty list. The better way of doing that in Python is probably:

def foo(n=None):
n = n is None and [42]

but if you're going to write that much, it's clearer to just go with
the canonical:

def foo(n=None):
if n is None:
n = [42]

but I digress :)
You are looking for

if (cal <= 0) or (fat <=0):

(Note: Parenthesis not required, but it may help you understand precedence of
evaluation.

That is a correct coding of the OP's intentions, although I would
think that the number of fat grams could indeed be zero for some
foods(?) so perhaps it would be more correct to say:

if cal <= 0 or fat < 0:

Good table, except for higher presedence being further down in the
table which might be confusing pedagogically.

-- bjorn
 
M

Michael L Torrie

that's the most incorrect thing i've heard all day!

if cal or fat <= 0 is parsed as if (cal) or (fat <= 0)

Which is exactly what he said. He also said that what the poster
probably wanted was

if cal <= 0 or fat <=0
 
S

Steven D'Aprano

Which is exactly what he said.

Heh, that was my first thought too, for about 3.2 milliseconds. And then
I realised that, no, he actually said:

'cal or fat' is evaluated first

that is, it was parsed like

if (cal or fat) <= 0

which is not correct.
 

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

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top