0

I am very new to pythonx3.6. I have a class with three methods that is suppose to calculate a tractor's calibration values in the same way a calculator does.

There are eight values in the equation. At first I wrote the code to calculate all eight values in one function, but because certain variables that are needed at the beginning is only calculated at the end of my function and I could not find an order for the equations that made for a valid effort. I decided to create a function for each variable to be calculated and return the function's values in all the other functions that they are needed.

Note: In this question I redacted the code to only two related functions containing the equations

The first method declares a dictionary data type with specific keys and values with two attributes "fact" and "tim" as the key and values that will be calculated depending on data in the equation that is available.

The second function calculates the 'fact' value and if it has the 'tim' value greater than 0 and not equal to null then it calculates the fact that represents an equation the calculates a factor that is a fraction of 60 seconds

The third function does the same as the second, except it calculates the time or 'tim' which is the time it took a tractor to drive a 100m and spray a field

I will be using a kivy textinput to insert the variables (but that is not for now) - but I first have to get the class functions to work. I tried from Idle by first creating an object of the class answ=Calibrated; then I tried answ.calibrationVar(90,""). I got an error:

Traceback (most recent call last):
   File "<pyshell#8>", line 1, in <module>
     faktor.calibrationVar(90,"")
   File "<pyshell#3>", line 3, in calibrationVar
     k = {'tim':self.tim, 'fact':self.fact}
 AttributeError: 'Calibrated' object has no attribute 'tim'

See my incorrect code below:

class Calibrated:
    def calibrationVar(self, fact, tim):
        k = {'tim':self.tim, 'fact':self.fact}

    def factorVar(self):
        if k['fact'] == "" and k['tim'] != "" and float(k['tim']):
            try:
                k['fact'] = float(k['tim']) / 60
                                return k['fact']
            except TypeError:

    def timeVar(self):
        if k['fact'] != '' and k['tim'] == "" and float(k['fact']):
            try:
                k['tim'] = 60 * float(k['fact'])
                return k['tim']
            except TypeError:

The result should be a dictionary k that holds the results of the functions equations e.g. if time = 90 then it would equate to a dictionary k{'tim':90, 'fact':1.5}. I can then print the results to a temporary database that will eventually output a pdf with the session results.

Can someone please tell me what I am doing wrong and what not?

...

I managed to solve my problem: I used a for loop and iterated over each equation until all values in the dictionary was solved.

@Patrick Artner: You were correct it was a scope problem and how I used the self thing. Thanks for the help.

My correct code:

class Calibrated:
    def calibrationVar(self, tim, fact):
        self.tim=tim
        self.fact=fact
        k = {'tim':tim, 'fact':fact}
        for x in range(2):
            if k['fact'] == "" and k['tim'] != "" and float(k['tim']):
                k['fact'] = float(k['tim']) / 60

            if k['fact'] != '' and k['tim'] == "" and float(k['fact']):
                k['tim'] = 60 * float(k['fact'])
        print(k['tim'],k['fact'])
Hmerman6006
  • 1,622
  • 1
  • 20
  • 45
  • Did you read about _scopes_? `k` is a local variable inside your function - not known in other functions. `def calibrationVar(self, fact, tim):` and `k = {'tim':self.tim, 'fact':self.fact}` - `tim and fact` are not members, they are local function parameters - why do you put `self.` in front? https://docs.python.org/3/tutorial/classes.html#class-objects – Patrick Artner Apr 02 '19 at 21:50
  • Possible duplicate of [Short description of the scoping rules?](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) – Patrick Artner Apr 02 '19 at 21:51
  • @Patrick Artner Yes, I did read the Scopes linked Q&A: When I change the scope from within the CalibrationVar function to Global and I also tried to Enclose it in the space between the class and first function, but I get a NameError that variables tim and fact is not defined. I used self because I want to create a place holder for the instance of the class so that I can access all the functions within the class. But I may be using it wrong. – Hmerman6006 Apr 03 '19 at 19:46
  • self.tim _does no exists_ where you use it. use `self.tim = tim` first if you want to store `tim` inside your class as property! `k` only exsits inside `def calibrationVar(self, fact, tim):` _not_ outside because it is function scope. use `self.k = {'tim': tim, 'fact': fact}` to create a member `self.k` inside your class instance that holds the values of tim and fact. Lookout for tutorials about python classes... you cn use `self.k` everywhere wher your functions of your class have a `self` instance thing. – Patrick Artner Apr 03 '19 at 19:47

0 Answers0