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'])