473,287 Members | 1,800 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,287 software developers and data experts.

while statements

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

Oct 16 '07 #1
5 1550
On Oct 16, 9:28 am, Shawn Minisall <trekker...@comcast.netwrote:
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

Oct 16 '07 #2
On 10/16/07, danfolkes <da*******@gmail.comwrote:
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
Oct 16 '07 #3
On Oct 16, 8:28 am, Shawn Minisall <trekker...@comcast.netwrote:
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

Oct 16 '07 #4
Shawn Minisall wrote:
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
Oct 16 '07 #5
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.

Oct 17 '07 #6

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

Similar topics

1
by: Brittany | last post by:
can someone explain to me what if else staments do and what while statements do.
33
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body -...
24
by: Andrew Koenig | last post by:
PEP 315 suggests that a statement such as do: x = foo() while x != 0: bar(x) be equivalent to while True:
4
by: James E Koehler | last post by:
I can't get the WHILE statement to work in MySQL. The version of MySQL that I am using is: Ver 12.16 Distrib 4.0.6-gamma, for Win95/Win98 (i32) running on Windows MX. Here is the relevant...
147
by: Michael B Allen | last post by:
Should there be any preference between the following logically equivalent statements? while (1) { vs. for ( ;; ) { I suspect the answer is "no" but I'd like to know what the consensus is
11
by: Rene | last post by:
Quick question, what is the point for forcing the semicolon at the end of the while statement? See example below: x = 0; do { x = x + 1; }while (x < 3); What's the point of having the...
3
by: yinzara | last post by:
I have the following trigger that calls a DB2 stored procedure: DROP TRIGGER GGWU.TRI_A_MULTI_PROP@ CREATE TRIGGER GGWU.TRI_A_MULTI_PROP AFTER INSERT ON GGWU.MULTIPLIER_PROPERTY REFERENCING ...
16
by: koutoo | last post by:
I start my code with some constants then a while statement. But I have some For statements towards the end within the While statement where I start getting some errors. I'm hoping I won't have to...
9
by: Logan Lee | last post by:
Hi. What's the difference between while and do while?
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.