This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
6年前关闭。
我被问到以下问题(有点冗长):
在物理学中,对于长度为L,初始角为a的摆,其在时间T时的水平位移X(T)由以下公式给出
X(T) = L × cos(A × cos(T × √9.8/L)) - L × cos(A)

编写一个接受两行输入的程序,第一行是L,第二行是a。输出应该是十行,给出X(0)X(1)X(2),…,X(9)的值。例如,如果第一行输入53.1,第二行输入0.8,则第一行输出为0.0,第二行输出为53.1*cos(0.8*cos(1*√9.8/53.1)) - 53.1*cos(0.8) ~ 2.6689
为了回答这个问题,我编写了以下代码:
from math import sqrt
from math import cos
L = float(input())
A = float(input())

def X(T):
   print(L*cos(A*cos(T*sqrt(9.8/L))-L*cos(A)))

for n in range(0, 9):
   X(n)

……但我的回答总是错的。我可能遗漏了一些括号,但我看不到在哪里。
我得到的结果是:
3.545012155898153
7.383727226708044
17.92714440725987
31.889478979714276
44.23118522394127
51.212404291669216
53.079364553814806
52.890770379027806
52.999922313121566

我应该得到的结果是:
0.0
2.6689070487226805
9.021742145820763
14.794542557581206
15.73774678328343
11.124903835610114
4.423693604072537
0.27377375601245213
1.295906539090336
6.863309996333497

最佳答案

你说得对,括号放错地方了。下面将修复它:

print(L*cos(A*cos(T*sqrt(9.8/L)))-L*cos(A))
                                ^ added    ^ removed

关于python - Python-摆式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15665154/

10-11 20:19