473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

if then elif

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.
Oct 10 '07 #1
7 1686
Shawn Minisall wrote:
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.
Oct 10 '07 #2
Shawn Minisall wrote:
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/byteof...recedence.html

-Larry

Oct 10 '07 #3
On Oct 10, 5:03 pm, Larry Bates <larry.ba...@websafe.comwrote:
Shawn Minisall wrote:
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/byteof...recedence.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)

Oct 10 '07 #4
On Oct 10, 11:03 pm, Larry Bates <larry.ba...@websafe.comwrote:
[...]
>
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:
Also read here:

http://www.ibiblio.org/g2swap/byteof...recedence.html

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

-- bjorn

Oct 10 '07 #5
ch************@gmail.com wrote:
>
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
>
Oct 11 '07 #6
On Wed, 10 Oct 2007 20:42:26 -0600, Michael L Torrie wrote:
ch************@gmail.com wrote:

>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.
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.
--
Steven.
Oct 11 '07 #7
Michael L Torrie wrote:
Which is exactly what he said.
Haha. Nevermind. You're right. A subtle distinction, isn't it.
>He also said that what the poster
probably wanted was

if cal <= 0 or fat <=0

Oct 11 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christopher M. Lusardi | last post by:
Hello, Is there anyway to do it other than using : #ifdef VAR1 ... #endif
13
by: Jim | last post by:
Could somebody tell me why I need the "elif char == '\n'" in the following code? This is required in order the pick up lines with just spaces in them. Why doesn't the "else:" statement pick this...
3
by: lostsoul | last post by:
Hi All, I am new in learning programming. I am very lost. I read all of the chapters over and over again and I am still lost. I have the program below that I need to modify it to add hint to it...
2
by: Charles Sullivan | last post by:
I'm trying to maintain some older C code (FOSS) which has been patched by various individuals over the years for portability to multiple Unix-like operating systems, to wit: Linux, SunOS, Solaris,...
2
by: juan-manuel.behrendt | last post by:
Hello together, I wrote a script for the engineering software abaqus/CAE. It worked well until I implemented a selection in order to variate the variable "lGwU" through an if elif, else...
2
bvdet
by: bvdet | last post by:
We are parametrically attaching a bent plate object to the side of a building column for support of a skewed beam. Given a relative rotation between the column and beam and which side of the column...
6
by: Neil Webster | last post by:
Hi all, I'm sure I'm doing something wrong but after lots of searching and reading I can't work it out and was wondering if anybody can help? I've got the following block of code: if a >= 20...
2
by: Kid Programmer | last post by:
Hello guys. I was wondering if you could use a thing in python in java. That is an elif statement. It is saying else if. It will execute a block of code if the condition in the if statement is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.