RedstoneGuy9248
RedstoneGuy9248

Reputation: 11

Function needs to be made before calling it, but variable has to be assigned before referenced

the get age function needs y

import math
import os
from tkinter import *

root = Tk()
root.title("DOG AGE CALCULATOR!!")


#define function to convert x.0 to x
def formatNumber(num):
  if num % 1 == 0:
    return int(num)
  else:
    return num
#print menu

def getAge():
    # Convert y
    if(y == 1):
        dy = int(15)
    elif(y == 2):
        dy = int(24)
    elif(y > 2):
        dy = int(((y-2)*5)+24)
    else:
        p = 2

    #convert m
    if(y == 0):
        dm = float(m*1.25)
    elif(y == 1):
        dm = float(m*0.75)
    elif(y > 1):
        dm =  float(m*(5/12))
    else:
        p = 2

    #add excess months to years
    if(dm > 12):
        dm = dm//12
        y = y+(dm%12)

    #add dog years and months
    dogym = float(dy+dm)
    #seperate dog month and year
    dogmonth, dogyears =  math.modf(dogym)
    dogmonths = dogmonth*12
    #formatting dog year and month
    dogmonths = formatNumber(dogmonths)
    dogyears = formatNumber(dogyears)
    dogyearstr = str("{} Dog Years".format(dogyears))
    dogmonthstr = str("{} Dog Months".format(dogmonths))
    for widget in frame.winfo_children():
        widget.destroy()
    ylabel = Label(root, text=dogyearstr)
    mlabel = Label(root, text=dogmonthstr)
    ylabel.pack()
    mlabel.pack()


title = Label(root, text="\/\/\!!DOG AGE CALCULATOR!!\/\/\\")
yentry = Entry(root)
mentry = Entry(root)
getagebutton = Button(root, text="Get Dog Age", command=getAge)
yentry.insert(0, "Dog's Age In Years")
mentry.insert(0, "Dog's Age In Months")
title.pack()
yentry.pack()
mentry.pack()
getagebutton.pack()
y = yentry.get()
m = mentry.get()

In this program, how do i fix the error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\name\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
    return self.func(*args)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\name\PycharmProjects\DAC\maintmp.py", line 19, in getAge
    if(y == 1):
       ^
UnboundLocalError: cannot access local variable 'y' where it is not associated with a value

(the file is maintmp.py)

Upvotes: 0

Views: 49

Answers (1)

JRiggles
JRiggles

Reputation: 6790

You should move the call to y = yentry.get() inside the getAge() function

def getAge():
    y = yentry.get()
    # Convert y
    if(y == 1):
        dy = int(15)
    elif(y == 2):
        dy = int(24)
    elif(y > 2):
        dy = int(((y-2)*5)+24)
    else:
        p = 2
    ...  # code omitted for brevity

You'll want to do a similar thing when getting the value of m. This way, the values are fetched from the entries when they're needed and can be updated accordingly.

As it's currently written, you're calling get() on your Entry widgets immediately after declaring them, so y and m will both be equal to None

You're getting the error UnboundLocalError: cannot access local variable 'y' where it is not associated with a value because you're trying to perform a comparison operation on y before it has any value (as far as getAge is concerned)

Also, unrelated to the error: the explicit conversions like int(15) are unnecessary. 15 is already an integer. You can just do dy = 15, dy = 24, etc. Since y is a string, you can just do the conversion on y with y = int(yentry.get()) (the same goes for m).

Then you don't need to worry about things like float(m*1.25) either, since any number multiplied by a float will return a float!

Upvotes: 1

Related Questions