Nov-25-2019, 04:41 PM
I think you can refactor this to avoid repetitions
import variables import math class Calculator(object): # common bits for both genders age = variables.age diabetes = variables.diabetes @property def a(self): a = 11.1122-0.9119*(math.log(variables.systolic_blood_pressure))-0.2767*(variables.smoking)-0.7181*(math.log(variables.cholesterol_ratio(variables.total_cholesterol,variables.HDL_cholesterol)))-0.5865*(variables.LVH) return a @property def mu1(self): mu1 = 4.4181 + self.m return mu1 def risk(self): sigma = math.exp(-0.3155-0.2784 * self.m) mu2 = (math.log(5) - self.mu1)/sigma p = 1-math.exp(-math.exp(mu2)) #p is the five year risk i.e. the intended output of the function return round(p*100,2) class MaleCalculator(Calculator): @property def m(self): return self.a-1.4792*(math.log(self.age))-0.1759*(self.diabetes) class FemaleCalculator(Calculator): @property def m(self): return self.a-5.8549+1.8515*( (math.log(self.age/74))**2)-0.3758*(self.diabetes) #calculates risks for male def risk_calculator_male(): return MaleCalculator().risk() def risk_calculator_female(): return FemaleCalculator().risk() def genderassignment(): #prompts user to enter gender and assigns correct calculator gender = input("Please enter gender, m for male and f for female. ") if gender == 'm': print(f"Your five year risk is: {risk_calculator_male()}% .") elif gender == 'f': print(f"Your five year risk is: {risk_calculator_female()}%. ") else: print("Does not compute!") return genderassignment() genderassignment()