提问人:Adrian Heffley 提问时间:8/3/2023 最后编辑:egleaseAdrian Heffley 更新时间:8/3/2023 访问量:34
如何完成此代码以计算地球的“逃逸速度”?
How do I complete this code to calculate the "escape velocity" of earth?
问:
我知道 != 应该更改为 ==,但是当我再次运行代码时,我认为它试图告诉我整个 [if mass == Decimal('0') or radius == Decimal('0'):] 命令不正确。我知道这段代码中至少还有一个语法错误,但是如果不先修复 [if mass == Decimal ('0') or radius == Decimal('0'):] 命令,我无法让 python 告诉我它是什么。
decimal import *
GRAVITATIONAL_CONSTANT = Decimal('6.67384E-11')
# Credit to http://codingwithnumbers.blogspot.com/ for this function
def escape_velocity(mass, radius):
'''Calculates the delta-V escape velocity.
Returns delta-V in meters per second for the escape
velocity for a given object given its mass in kilograms
and its radius in meters.'''
#IMPORTANT: mass and radius should be checked to see if they are exactly equal to zero, using the correct comparison operator
# If either are exactly equal to 0, this function will return None
if mass != Decimal('0') or radius != Decimal('0'):
return None
# Calculate the result using correct formulas
# NOTE: Syntax errors on one line may mistakenly cause an error to appear on another
result = Decimal((2 * GRAVITATIONAL_CONSTANT * mass) / radius
result = result.sqrt()
return result
earth_mass_in_kg = Decimal('5.9736E24')
earth_radius_in_meters = Decimal('6371000')
print("The escape velocity of Earth is {} m/s".format(round(escape_velocity(earth_mass_in_kg, earth_radius_in_meters))))
答: 暂无答案
评论