万有引力定律 - 函数计算力 F=((G * m1 * m2) / (d ** 2))
Universal Law of Gravity - Functions calculate Force F=((G * m1 * m2) / (d ** 2))
我目前正在努力学习 Python,这样我就可以为我社区的 children 提供新的机会。
我被函数卡住了,我正在尝试使用万有引力方程计算 space 两个物体之间的力。我不确定我做错了什么,我觉得我什至可能不会调用 m2,所以它可以计算,或者我可能需要制作特定于行星的函数,然后填充方程式,并需要用户输入?这是我目前所拥有的:
def gravitionalForce (m1, m2, d): #d=Distance
G= 6.673*(10**-11) #Gravity
m1 = 1.9891 * (10 ** 30) # Sun
m2.Jupiter= 1.8986*(10**27)#Jupiter
m2.Saturn = 5.68646*(19**26)#Saturn
m2.Neptune = 1.0243*(10*26)#Neptune
d.Jupiter=7.41(10**11)
d.Saturn=1.35(10**12)
d.Neptune=4.45*(10**12)
F=((G * m1 * m2) / (d ** 2))
return F
print (input("what planet you want to calculate? Jupiter, Saturn, Neptune? : ")
我正在通过看书和看 youtube 来学习,我知道我的理解有漏洞。任何帮助或方向将不胜感激。
此代码存在一些不同的问题。我在下面对其进行了一些清理,希望这有助于让您走上正确的轨道
planets = {
'Jupiter': {
'm2': 1.8986,
'd': 7.41
}
}
def gravitionalForce (m1, planet): #d=Distance
G = 6.673*(10**-11) #Gravity
m1 = 1.9891 * (10 ** 30) # Sun
planet_constants = planets[planet]
F=((G * m1 * planet_constants['m2']) / (planet_constants['d'] ** 2))
return F
planet = input("what planet you want to calculate? Jupiter, Saturn, Neptune? : ")
print(gravitionalForce(100, planet))
请注意,行星常量已移至 dictonary 并且 m1
变量被传递为常量 100
我会这样做:
# define your constants up here
values = {
"Jupiter": { "Mass": 1.8986*(10**27), "Distance": 7.41*(10**11) },
"Neptune": { "Mass": 1.0243*(10*26), "Distance": 4.45*(10**12) },
"Saturn:": { "Mass": 5.68646*(19**26), "Distance": 1.35*(10**12) }
}
def gravitationalForce (m1, m2, d):
g = 6.673*(10**-11)
f = (g * m1 * m2) / (d ** 2)
return f
planet = input("what planet you want to calculate? Jupiter, Saturn, Neptune? : ")
sun_mass = 1.9891*(10**30)
# get the values based on the input from the dictionary above
planet_mass = values[planet]["Mass"]
distance = values[planet]["Distance"]
# pass those arguments into your function to calculate it
print(gravitationalForce(sun_mass, planet_mass, distance))
需要指出的几个问题:
d.Jupiter=7.41(10**11)
这会给出错误 TypeError: 'float' object is not callable
因为你调用了像 function_name()
这样的函数所以当你使用 7.41(10**10)
它认为你正在尝试将 7.41
作为函数调用。不要忘记在 7.41
和 (10**11)
之间添加一个 *
。
m2.Jupiter= 1.8986*(10**27)
这会给出错误 AttributeError: 'float' object has no attribute 'Jupiter'
因为你不能在不首先定义参数 m2
a 属性 的情况下分配参数.如果您想有多个选项,请像我上面那样使用字典或创建一个单独的 class。
print (input("what planet you want to calculate? Jupiter, Saturn, Neptune? : ")
这里您只是打印您输入的值。要获取输入值,请将其分配给变量。然后你必须在某个时候用你的参数调用函数 gravitationalForce
。不要在函数内定义参数,将它们放在函数外部,然后将它们传递给函数。
我目前正在努力学习 Python,这样我就可以为我社区的 children 提供新的机会。
我被函数卡住了,我正在尝试使用万有引力方程计算 space 两个物体之间的力。我不确定我做错了什么,我觉得我什至可能不会调用 m2,所以它可以计算,或者我可能需要制作特定于行星的函数,然后填充方程式,并需要用户输入?这是我目前所拥有的:
def gravitionalForce (m1, m2, d): #d=Distance
G= 6.673*(10**-11) #Gravity
m1 = 1.9891 * (10 ** 30) # Sun
m2.Jupiter= 1.8986*(10**27)#Jupiter
m2.Saturn = 5.68646*(19**26)#Saturn
m2.Neptune = 1.0243*(10*26)#Neptune
d.Jupiter=7.41(10**11)
d.Saturn=1.35(10**12)
d.Neptune=4.45*(10**12)
F=((G * m1 * m2) / (d ** 2))
return F
print (input("what planet you want to calculate? Jupiter, Saturn, Neptune? : ")
我正在通过看书和看 youtube 来学习,我知道我的理解有漏洞。任何帮助或方向将不胜感激。
此代码存在一些不同的问题。我在下面对其进行了一些清理,希望这有助于让您走上正确的轨道
planets = {
'Jupiter': {
'm2': 1.8986,
'd': 7.41
}
}
def gravitionalForce (m1, planet): #d=Distance
G = 6.673*(10**-11) #Gravity
m1 = 1.9891 * (10 ** 30) # Sun
planet_constants = planets[planet]
F=((G * m1 * planet_constants['m2']) / (planet_constants['d'] ** 2))
return F
planet = input("what planet you want to calculate? Jupiter, Saturn, Neptune? : ")
print(gravitionalForce(100, planet))
请注意,行星常量已移至 dictonary 并且 m1
变量被传递为常量 100
我会这样做:
# define your constants up here
values = {
"Jupiter": { "Mass": 1.8986*(10**27), "Distance": 7.41*(10**11) },
"Neptune": { "Mass": 1.0243*(10*26), "Distance": 4.45*(10**12) },
"Saturn:": { "Mass": 5.68646*(19**26), "Distance": 1.35*(10**12) }
}
def gravitationalForce (m1, m2, d):
g = 6.673*(10**-11)
f = (g * m1 * m2) / (d ** 2)
return f
planet = input("what planet you want to calculate? Jupiter, Saturn, Neptune? : ")
sun_mass = 1.9891*(10**30)
# get the values based on the input from the dictionary above
planet_mass = values[planet]["Mass"]
distance = values[planet]["Distance"]
# pass those arguments into your function to calculate it
print(gravitationalForce(sun_mass, planet_mass, distance))
需要指出的几个问题:
d.Jupiter=7.41(10**11)
这会给出错误TypeError: 'float' object is not callable
因为你调用了像function_name()
这样的函数所以当你使用7.41(10**10)
它认为你正在尝试将7.41
作为函数调用。不要忘记在7.41
和(10**11)
之间添加一个*
。m2.Jupiter= 1.8986*(10**27)
这会给出错误AttributeError: 'float' object has no attribute 'Jupiter'
因为你不能在不首先定义参数m2
a 属性 的情况下分配参数.如果您想有多个选项,请像我上面那样使用字典或创建一个单独的 class。print (input("what planet you want to calculate? Jupiter, Saturn, Neptune? : ")
这里您只是打印您输入的值。要获取输入值,请将其分配给变量。然后你必须在某个时候用你的参数调用函数gravitationalForce
。不要在函数内定义参数,将它们放在函数外部,然后将它们传递给函数。