473,387 Members | 1,374 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.

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 1553
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?
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.