Python Forum

Full Version: coding help -python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#!/usr/bin/env python3

import sys
import math

print("BMI calculator")

#Defining BMI Class
class Bmi():
#Setting Values for BMI Class
   def __init__(self,name="", stones=0, pounds=0, kilos=0, feet=0, inches=0, centimeters=0):
           self.name = name
           self.stones = stones
           self.pounds = pounds
           self.kilos = kilos
           self.feet = feet
           self.inches = inches
           self.centimeters = centimeters

   def metric_insert(self):
       continueLoop = "Enter"
       while continueLoop == "Enter":
           print('enter')
           try:
               self.name = input('Please enter your name: ')
               self.metric = input('Enter "Imperial" or "Metric": ')
               if self.metric == "Imperial" or self.metric == "Metric":
                   continueLoop = "Finish"
               else:
                   print('Please Enter exactly "Imperial" or "Metric": ')
           except TypeError:
               print('An Error flew by!')

   def input_height(self):
   #Inputs for Height Figures unless exceptions are entered:
           try:
               if self.metric == "Imperial":
                   self.feet = int(input("Enter Feet: "))
                   self.inches = int(input("Enter Inches: "))
                   self.calcinches = self.feet * 12
                   self.calccms = (self.calcinches + self.inches) * 2.54
                   value = self.calccms
               elif self.metric == "Metric":
                   self.centimeters = int(input("Enter Centimeters: "))
                   value = self.centimeters
               if value ==0:
                   print("You need to enter at least 1 Height Number")
               elif value < 0:
                   print("Height must be positive")
               elif value > 272:
                   print("Please enter valid height numbers")
               else:
                   continueLoop = "Finish"
           except ValueError:
               print("Please enter Integers only")

   def input_weight(self):
   #Inputs for Weight Figures unless exceptions are entered

           try:
               if self.metric == "Imperial":
                   self.stones = int(input("Enter Stones: "))
                   self.pounds = int(input("Enter Pounds: "))
                   self.calcpounds = self.stones * 14
                   self.weight_output = ((self.calcpounds + self.pounds) * 0.453592369999995)
                   value = self.weight_output
               elif self.metric == "Metric":
                   self.kilos = int(input("Enter Kilos: "))
                   value = self.kilos
               if value ==0: print("You need to enter at least 1 Height Number")
               elif value < 0: print("Weight must be positive")

               elif value > 272: print("Please enter valid height numbers")
               else:continueLoop = "Finish"
           except ValueError:
               print("Please enter Integers only")

   def calc_bmi(self):
    #Calculate bmi.
        if self.metric == "Imperial":
            bmi= (self.calcpounds / (self.calcinches * self.calcinches)) * 703
        else:
            bmi=(self.kilos / (self.calccms * self.calccms))
        return round(bmi)

   def bmi_analysis(bmi):
       if bmi < 18.5:
           return( "and you are underweight")
       elif bmi <= 18.5 and bmi <= 24.9:
           return( "and you are Normal(Healthy weight)")
       elif bmi <= 25 and bmi <= 29.9:
           return( "and you are Overweight")
       elif bmi > 30:
           return( "and you are Obese")

   def main(self):
          self.metric_insert()
          self.input_height()
          self.input_weight()
          self.calc_bmi()
          self.bmi_analysis()

   main()

   print("Hello", name)
   print("Your Body mass Index is ", calc_bmi(), bmi_analysis())
#######################################################
# This is the program for the BMI calculator, I should be able to enter name height and weight.
Can anyone please let me know, what wrong am I doing here. Help much appreciated.
First, need to instantiate the class. In line 102, add
bmi = BMI()
then in 103 make it bmi.main(), 105 should have bmi.name instead of just name

You call calc_bmi() in 100 but do nothing with the return value. Either store bmi within the object or save it.
You then call bmi_analysis in 101 without an argument, but in the function definition it is supposed to be sent "bmi". You could instead combine 100 and 101 with
self.bmi_analysis(self.calc_bmi())
Fix those and it should work, though I have no idea how many stones I am.
Thanks for the help jefsummers. It helped me.