恢复系数

2024-06-17 08:14:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个变量的“恢复系数”(cor)和“以米为单位的初始高度”(h)。我想找出“行程米数”(th)是cor*h+cor*h+。。。。相同的乘法,直到米数小于0.10米。我所做的如下:

cor = float(input("Enter coefficient of restitution : "))
h = float(input("Enter initial height in meters : "))
nob = 0
th = h
while (h >= 0.10):
    nob += 1
    h *= cor    
    th += h   
print("Meters traveled : {0:.2f}".format(th))
print("Number of bounces : ", nob)

我的结果是,我的结果是8.44米。但是,我有正确的反弹次数(13)。在

为什么我的答案会产生错误的代码?在


Tags: ofinput高度单位floatprintenter行程
1条回答
网友
1楼 · 发布于 2024-06-17 08:14:21

在指定th时,似乎没有考虑球的上下移动。球下降8米,那么cor意味着球会上升5.6米,也会下降5.6米。改变你的th+=h*2,这就更接近你书中的答案了。在

cor = float(input("Enter coefficient of restitution : "))
h = float(input("Enter initial height in meters : "))
nob = 0
th = h
while (h >= 0.10):
    nob += 1
    h *= cor    
    th += h*2   
print("Meters traveled : {0:.2f}".format(th))
print("Number of bounces : ", nob)

相关问题 更多 >