You have to use a while loop to allow the user to run up to 3 BMI calculations. If
they decide to quit before by entering the letter 'q' for 'quit', they should be allowed
to quit the program. In all other cases (i.e. any other input) let them continue with the next BMI calculation. However, after 3 calculations the program should terminate.
2. If a weight < 0 or weight > 500 lbs is entered by the user, a specific helpful error message is
displayed and the program bypasses all other code (i.e., no computation or result display)
and goes back to the very beginning where the message “Program computes your BMI” is printed.
(See the sample program bmi1.py on the second page.)
3. If a height < 0 is entered another specific error message is displayed and the program bypasses
all other code and goes back to the very beginning where the message “Program computes your
BMI” is printed.
4. Based on the BMI value you compute, use the table provided on the Web site to display
if the person is underweight, normal weight, etc. (hint: sent this BMI value to a function to
determine the correct message to display and have it display it there.)
5. As part of your implementation, use functions and limit code duplication where possible. For
example, you will have a method named main, and in it you will have your main loop with the
if‐statements needed for error checking. Try not to fill up your main loop with lots of code, try to call methods instead if possible.
All I want to know is if I am supposed to have the program prompt the user to type in 'q'
after each calculation or just have all 3 calculations calculate one after the other and
after the 3rd calculation the program executes. If someone can interpret #1 for me at the
top that would be great. Here is the complete code I have written:
#!/usr/bin/env python # If user types in q, program terminates # If user types in other than q, program # continues and calcualtes bmi def main(): quit = False count = 0 answer = raw_input('\nEnter "q" to quit: ') if answer == 'q': quit = True # Caclulates bmi 3 times prompting user to type in 'q' # or another letter to continue with bmi calculation while (not quit and count < 3): print '-------------------------' print 'Program computes your BMI' print '-------------------------' weight = float(input('Enter weight in pounds: ')) height = float(input('Enter height in inches: ')) # Weight cannot be less than 0 or greater than 500 if weight <= 0 or weight > 500: print 'Weight cannot be less than 0 or greater than 500' continue # Height cannot be less than or equal to 0 inches elif height <= 0: print 'Height cannot be less than 0' continue else: bmi = (weight / (height * height)) * 703.0 print 'Your BMI is %.2f' % bmi if bmi <= 18.5: print 'Your weight status is Underweight' elif bmi >= 18.5 and bmi <= 24.9: print 'Your weight status is Normal weight' elif bmi >= 25 and bmi <= 29.9: print 'Your weight status is Overweight' elif bmi >= 30: print 'Your weight status is Obese' # increments bmi calculation by 1 with a total of 3 bmi calculations count = count + 1 if count < 3: answer = raw_input('\nEnter "q" to quit: ') if answer == 'q': quit = True main() raw_input('\n<enter> to exit')
This post has been edited by j1_osu2002: 11 March 2010 - 07:53 PM