如何解决我的方法返回值链接处的地址
这是一个不属于现场培训的练习,我不明白为什么当我想显示我的卫星类的能量方法的返回值时,我在控制台上收到一个地址。
>Zoe satellite speed = 40.0m / s.
<bound method Satellite.energie of <__ main __. Satellite object at 0x01A5E610 >>
Zoe satellite speed = 70.0m / s.
<bound method Satellite.energie of <__ main __. Satellite object at 0x01A5E610 >>
这是我的代码:
class Satellite (object):
"" "Satellite for instantiating objects simulating satellites
artificial launched into space,around the earth. "" "
def __init __ (self,name,mass = 100,speed = 0):
self.name = name
self.mass = mass
speed choke = speed
def impulse (self,force,duration):
"" "will vary the speed of the satellite." ""
speed self = speed self + (force * duration) / mass self
def energy (self):
"" "will refer to the program calling the kinetic energy value of the satellite" ""
return_val = self.mass * self.speed ** 2/2
return return_val
def display_speed (self):
"" "will display the name of the satellite and its current speed." ""
print ("satellite speed {0} = {1} m / s.". format (self.name,self.speed))
s1 = Satellite ('Zoe',mass = 250,speed = 10)
s1.pulse (500,15)
s1.display_speed ()
print (s1.energy)
s1.pulse (500,15)
s1.display_speed ()
print (s1.energy)
解决方法
你的 energy
方法是错误的,它应该给出了一个语法错误:
def energy (self):
"" "will refer to the program calling the kinetic energy value of the satellite" ""
return_val = self.mass * self.speed ** 2/2
return return_val
更不用说,您还需要调用 energy
方法:
s1.pulse (500,15)
s1.display_speed ()
print(s1.energy())
s1.pulse (500,15)
s1.display_speed ()
print(s1.energy())
您试图覆盖 return
语句并将其用作变量,这是错误的。您的代码中还有其他错误输入/问题,但这个似乎绝对是主要问题。
您不是调用函数 Satellite.energy()
,而是尝试访问属性 Satellite.energy
- 如果将其更改为 print(s1.energy())
,它应该会正确显示!
请注意,您的某些代码在此处格式错误,因此无法运行 - 这对我有用:
class Satellite (object):
"" "Satellite for instantiating objects simulating satellites artificial launched into space,around the earth. "" "
def __init__ (self,name,mass = 100,speed = 0):
self.name = name
self.mass = mass
self.speed = speed
def impulse (self,force,duration):
"" "will vary the speed of the satellite." ""
return self.speed + (force * duration) / self.mass
def energy (self):
"" "will refer to the program calling the kinetic energy value of the satellite" ""
return self.mass * self.speed ** 2/2
def display_speed (self):
"" "will display the name of the satellite and its current speed." ""
print ("satellite speed {0} = {1} m / s.". format (self.name,self.speed))
s1 = Satellite ('Zoe',mass = 250,speed = 10)
s1.impulse (500,15)
s1.display_speed ()
print (s1.energy())
s1.impulse (500,15)
s1.display_speed ()
print (s1.energy())
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。