Python Forum
TypeError: Not supported between instances of 'function' and 'int'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: Not supported between instances of 'function' and 'int'
#4
So now I have finished the code, but still need some help tidying things up a little.

import variables
import math

class common(object):   # common bits for both genders

    def a():
     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

    def mu1():
        mu1 = 4.4181+m
        return mu1

  #calculates risks for male
def risk_calculator_male():

        age = variables.age
        diabetes = variables.diabetes
        a = common.a()
        mu1 = common.mu1()
        m = a-1.4792*(math.log(age))-0.1759*(diabetes)
        #mu1 = 4.4181 + m  #commented out to demonstrate error
        sigma = math.exp(-0.3155-0.2784*m)
        mu2 = (math.log(5)-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)

def risk_calculator_female():

        age = variables.age
        diabetes = variables.diabetes
        a = common.a()
        mu1 = common.mu1()
        m = a-5.8549+1.8515*((math.log(age/74))**2)-0.3758*(diabetes)
        #mu1 = 4.4181+m
        sigma = math.exp(-0.3155-0.2784*m)
        mu2 = (math.log(5)-mu1)/sigma
        p = 1-math.exp(-math.exp(mu2))

        return round(p*100,2)

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()
As you can see from the code above there are many common elements between the functions risk_calculator_male and risk_calculator_female, being a, mu1, sigma and mu2 (in fact everything is common except m). I want to try and put all the common bits in the common class but when I do it like above, I get an error message saying m is not defined in class common:

Error:
Traceback (most recent call last): File "framingham.py", line 82, in <module> genderassignment() File "framingham.py", line 46, in genderassignment print(f"Your five year risk is: {risk_calculator_male()}% .") File "framingham.py", line 20, in risk_calculator_male mu1 = common.mu1() File "framingham.py", line 11, in mu1 mu1 = 4.4181+m NameError: name 'm' is not defined
I can't make m global as it is different in both functions ( and I've read its bad practice anyway) and I can't define m in the common module because it depends on what gender the user enters. Is there a way around this, or do I just need to type it out in each function, like this:
import variables
import math

class common(object):   # common bits for both genders

    def a():
     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

  #calculates risks for male
def risk_calculator_male():

        age = variables.age
        diabetes = variables.diabetes
        a = common.a()
        mu1 = common.mu1()
        m = a-1.4792*(math.log(age))-0.1759*(diabetes)
        mu1 = 4.4181 + m
        sigma = math.exp(-0.3155-0.2784*m)
        mu2 = (math.log(5)-mu1)/sigma
        p = 1-math.exp(-math.exp(mu2))
        return round(p*100,2)

def risk_calculator_female():

        age = variables.age
        diabetes = variables.diabetes
        a = common.a()
        mu1 = common.mu1()
        m = a-5.8549+1.8515*((math.log(age/74))**2)-0.3758*(diabetes)
        mu1 = 4.4181+m
        sigma = math.exp(-0.3155-0.2784*m)
        mu2 = (math.log(5)-mu1)/sigma
        p = 1-math.exp(-math.exp(mu2))

        return round(p*100,2)

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()
Thanks again.
Reply


Messages In This Thread
RE: TypeError: Not supported between instances of 'function' and 'int' - by palladium - Nov-25-2019, 02:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using curve_fit to optimize function (TypeError) Laplace12 4 4,054 Aug-30-2021, 11:15 AM
Last Post: Larz60+
  [Solved] TypeError when calling function Laplace12 2 4,034 Jun-16-2021, 02:46 PM
Last Post: Laplace12
  Sort Function: <' not supported between instances of 'float' and 'tuple' quest 2 10,118 Apr-30-2021, 07:37 PM
Last Post: quest
Exclamation TypeError: '>=' not supported between instances of 'int' and 'str' helpme1 11 11,900 Mar-11-2021, 11:13 AM
Last Post: helpme1
  Type error: '>' not supported between instances of 'NoneType' and 'int' spalisetty06 1 11,762 Apr-29-2020, 06:41 AM
Last Post: buran
  TypeError: '<' not supported between instances of 'str' and 'int' Svensation 5 12,207 Jan-20-2020, 08:12 PM
Last Post: buran
  TypeError: '>=' not supported between instances of 'str' and 'int' AsadZ 8 15,923 Aug-20-2019, 11:45 AM
Last Post: ThomasL
  '>' not supported between instances of 'str' and 'int' graham23s 2 4,978 May-11-2019, 07:09 PM
Last Post: micseydel
  Newbie Question re "TypeError: '<' not supported between instances of 'list' and 'int sr12 8 17,662 Apr-11-2019, 08:19 PM
Last Post: sr12
  '<' not supported between instances of 'str' and 'int' jayaherkar 1 8,726 Apr-09-2019, 03:25 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020