Benjamin Inverno
Benjamin Inverno

Reputation: 41

Referring to Previous Statements in python

def bmicalculation(self):
        bmiheight=self.heightcm.get()
        print(bmiheight)
        bmiweight=self.weightkg.get()
        bmi= float((bmiweight)/((bmiheight / 100)**2))
        print(bmi)
        self.label1=Label(self.master,text='Your BMI is %.2f' % bmi).grid(row=5,column=0)

        if bmi <= 18.5:
            self.label2=Label(self.master,text='This places you in the underweight group.',fg='blue').grid(row=6,column=0)
            totalindex = 'underweight'
        elif bmi >18.5 and bmi <25:
            self.label3=Label(self.master,text='This places you in the healthy weight group.',fg='green').grid(row=6,column=0)
            totalindex = 'healthy'
        elif bmi >= 25 and bmi < 30:
            self.label4=Label(self.master,text='This places you in the overweight group.',fg='orange').grid(row=6,column=0)
            totalindex = 'overweight'
        elif bmi >=30:
            self.label5=Label(self.master,text='This places you in the obese group.',fg='red').grid(row=6,column=0)
            totalindex = 'obese'

        if bmi >0 and bmi <999999999999999999999:
            self.button6=Button(self.master,text="Store Data",fg='red',command=self.dynamic_data_entry).grid(row=8,column=0)


    def dynamic_data_entry(self):
        #this is what adds the data to the database. Bmi has to be changed to the value of bmi and weightclass has to be change to the weightclass
        timestamp = str(datetime.datetime.now().date())
        bodymassindex = '20'
        weightclass = str(totalindex)
        c.execute("INSERT INTO BMIStorage (timestamp, bodymassindex, weightclass) VALUES (?, ?, ?)",(timestamp, bodymassindex, weightclass))
        conn.commit()

I want to refer back to the term i created bmi in my second definition def dynamic_data_entry. How can i do this?

Also, how could i turn the weight groups given by the BMI into a string that I can then put into a database created by SQLite?

Upvotes: 0

Views: 45

Answers (2)

ffledgling
ffledgling

Reputation: 12140

If your second function def dynamic_data_entry is inside the def bmicalculation(self) function, the bmi variable should be accessible inside the function anyway.

If it's not, you need to pass it as a parameter to def dynamic_data_entry

Upvotes: 0

Lafexlos
Lafexlos

Reputation: 7735

Make bmi a class variable something like self.bmi. Same goes for totalindex as well.

bmi= float((bmiweight)/((bmiheight / 100)**2))
self.bmi = bmi

.
.
.
elif bmi >=30:
    self.label5=Label(self.master,text='This places you in the obese group.',fg='red').grid(row=6,column=0)
    totalindex = 'obese'

self.totalindex = totalindex

With these now you can access self.bmi and self.totalindex in your dynamic_data_entry method.

Upvotes: 1

Related Questions