So I have a class with some functions. I want to use a function in another function to calculate the fuelconsumption.
I have the attribute self.consumption, which is calculated within the function Calculate_consumption.
Now I want to write a new function, which is updating the kilometer counter and also calculating if you are driving efficient.
So, I want to calculate the consumption by using the function Claculate_consumption and then see if it is bigger then 8 or not.
Well I tried to just write the function as I have found it here on Stackoverflow: How do you call a function in a function?
But this solution somehow does not work. Maybe somebody can point out my mistake.
class Car:
def __init__(self, kmDigit):
self.kmDigit = int(kmDigit)
self.Max = 8
self.consumption = 0
def Claculate_consumption(self, Liter, km):
self.consumption += (Liter/km)*100
return round(self.consumption, 2)
def Refuel(self,Liter, km):
self.kmDigit += km
print self.kmDigit
a = Claculate_consumption(Liter, km)
if a > self.Max:
b = self.consumption - self.Max
print 'Your fuel consumption is too high!'
else:
print 'Nice!'
I am getting a **NameError** in line 14, because Calculate_consumption is somehow a global name.
a = self.Claculate_consumption(Liter, km)