我正在使用Python开发自己的东西来学习等等。唯一的问题是,我无法在方法中返回或打印变量。
import math
class BulletCalc():
bullet_type = ''
velocity = 0.0
zero = 0.0
muzzle_height = 0.0
target_angle = 0.0
target_distance = 0.0
gravity = 9.8
def time_air(self):
sqrtvalue = 2*self.muzzle_height
sqrtvalue = sqrtvalue/self.gravity
timein = sqrt(sqrtvalue)
def drop(self, time_air):
final_drop = self.muzzle_height - 1/2
final_drop = final_drop * self.gravity
final_drop = final_drop * time_air.timein
final_drop = final_drop**2
print(final_drop)
Fiftyc = BulletCalc()
Fiftyc.bullet_type = '50 Caliber'
Fiftyc.velocity = 928.0
Fiftyc.muzzle_height = 0.3
我正在尝试使用drop方法打印Final Drop。它不会打印任何东西。我不知道为什么。对不起,我是Python新手。谢谢!我已经尝试过返回最后一滴,以及所有这些
答案 0 :(得分:1)
你一定不要忘记实际调用方法!:
Fiftyc.drop()
使用time_air
编辑: 将您的代码更改为此
def time_air(self):
sqrtvalue = 2*self.muzzle_height
sqrtvalue = sqrtvalue/self.gravity
timein = sqrt(sqrtvalue)
return timein
def drop(self, time_air):
final_drop = self.muzzle_height - 1/2
final_drop = final_drop * self.gravity
final_drop = final_drop * time_air.timein
final_drop = final_drop**2
print(final_drop)
Fiftyc = BulletCalc()
Fiftyc.bullet_type = '50 Caliber'
Fiftyc.velocity = 928.0
Fiftyc.muzzle_height = 0.3
Fiftyc.drop(Fiftyc.time_air()) # call drop() with the return value of time_air()
请注意time_air
方法中添加的return语句,并使用该返回值在最后一行调用drop
方法
答案 1 :(得分:0)
简单地调用 Fiftyc.drop()将无法解决您的问题。
有很多问题:
答案 2 :(得分:0)
代码工作。你要做的事情应该是这样的 代码吼叫。甚至可以做出改进。
import math
class BulletCalc(object):
def __init__(self, bullet_type, velocity, muzzle_height):
"""
Class constructor. Should pass the values here """
self.bullet_type = bullet_type
self.velocity = velocity
self.muzzle_height = muzzle_height
self.gravity = 9.8
def time_air(self):
sqrtvalue = 2*self.muzzle_height
sqrtvalue = sqrtvalue/self.gravity
timein = math.sqrt(sqrtvalue)
return timein
def drop(self):
final_drop = self.muzzle_height - 1/2
final_drop = final_drop * self.gravity
final_drop = final_drop * self.time_air()
final_drop = final_drop**2
print(final_drop)
if __name__ == "__main__":
Fiftyc = BulletCalc(
bullet_type = '50 Caliber',
velocity = 928.0,
muzzle_height = 0.3 )
Fiftyc.drop()