5 Replies - 10269 Views - Last Post: 12 March 2010 - 04:34 PM Rate Topic: -----

#1 j1_osu2002  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-February 09

bmi calculation

Posted 11 March 2010 - 07:49 PM

I was wondering if my code is written correctly. Here is the description of what I am supposed to do:

1.
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


Is This A Good Question/Topic? 1
  • +

Replies To: bmi calculation

#2 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: bmi calculation

Posted 11 March 2010 - 11:19 PM

If it was me, I would put all the prompts in a loop, then prompt at (as the last prompt) whether to calculate another.

You could, once you get the value, check if the value is 'q'. If it is break, otherwise just continue on.
Was This Post Helpful? 0
  • +
  • -

#3 j1_osu2002  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-February 09

Re: bmi calculation

Posted 12 March 2010 - 10:08 AM

View Postzim1985, on 11 March 2010 - 10:19 PM, said:

If it was me, I would put all the prompts in a loop, then prompt at (as the last prompt) whether to calculate another.

You could, once you get the value, check if the value is 'q'. If it is break, otherwise just continue on.


Thanks for the reply. The program is actually supposed to terminate after the 3rd bmi calculation. What I am not understanding from one of the requirements number 1: 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.

should I leave out or keep:
if count < 3:
        answer = raw_input('\nEnter "q" to quit: ')
        if answer == 'q':
            quit = True


This post has been edited by j1_osu2002: 12 March 2010 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

#4 j1_osu2002  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 57
  • Joined: 09-February 09

Re: bmi calculation

Posted 12 March 2010 - 04:13 PM

How do I send my bmi value to a function. Here is the complete code for bmi that I have hand coded:

#!/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 or equal to 0 or greater than 500
      if weight <= 0 or weight > 500:
          print 'Weight cannot be less than 0 or greater than 500'

# Goes back to beginning of while loop
          continue

# Height cannot be less than or equal to 0 inches.
# Reason for <= is you can't divide 0/0.
      elif height <= 0:
          print 'Height cannot be less than or equal to 0'
          continue

# Calculates bmi
      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')



and this is what I want to send to it's own function without messing everything up:
# Calculates bmi
      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'



Any help is greatly appreciated! :)
Was This Post Helpful? 0
  • +
  • -

#5 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: bmi calculation

Posted 12 March 2010 - 04:16 PM

Ok, do all the calculations, check if the 'q' is entered, and then increment a count variable.

In the parameter for the while loop...
while(count <= 3 and answer != 'q')


Everything else should work the same way.
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2871
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: bmi calculation

Posted 12 March 2010 - 04:34 PM

Yah, just make new method like so:
def printBMI(weight, height):
    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'



Then just do this:
      elif height <= 0:
          print 'Height cannot be less than or equal to 0'
          continue

# Calculates bmi
      else:
          printBMI(int(weight), int(height))



Oh and topics merged. Please don't creat duplicate topics. >:(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1