#python #object
#python #объект
Вопрос:
Это упражнение, которое не является частью обучения на сайте, я не понимаю, почему, когда я хочу отобразить возвращаемое значение метода energy моего класса satellite, я получаю адрес на консоли.
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)
Комментарии:
1. Вам нужно вызвать / вызвать метод, например
s1.energy()
.2. Есть так много проблем с кодом, который вы публикуете. Он никогда не запустится и не даст того результата, который вы показываете. Пожалуйста, опубликуйте свой фактический код, хотя некоторые ответы уже указывают в правильном направлении.
Ответ №1:
Ваш 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
оператор и использовать его как переменную, что просто НЕПРАВИЛЬНО. В вашем коде также есть другие опечатки / проблемы, но эта определенно кажется основной проблемой.
Комментарии:
1. Спасибо, я изменил код, но он по-прежнему возвращает адрес
2. Опубликованный вами код даже не запускается, вы уверены, что опубликовали правильный код?
Ответ №2:
Вместо вызова функции 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())