I don’t understand why it has two difference result. Please help me, to explain these results.
This is my codesorce:
# coding: iso-8859-2
class Satelite:
def __init__(self, name, mass=100, speed=0):
self.name, self.mass, self.speed= name, mass, speed
def lokes(self, power, time):
self.speed=self.speed+ power*time/ self.mass
def energy(self):
return self.mass* self.speed**2/2
def out_speed(self):
print "%s Satelites's speed = %s m/s" %(self.name, self.speed)
# Tesztprogram:
s1=Muhold('Zoe', mass=250, speed=10)
s1.lokes(500,15)
s1.out_speed()
print s1.energy()
s1.lokes(500,15)
s1.out_speed()
print s1.energy()
These are the results:
Zoe Satelite speed= 40 m/s
200000
Zoe Satelite speed= 70 m/s
612500
That’s because you’re modifying a state variable in the middle.
Since
speedis changed, and theenergymethod depends onspeed, then the output ofenergyshould change every time you call lokes with values other than0.