使用公式KE=1/2*m*(v**2)
,如何在给出KE时找到速度。
例如,质量为10
,KE为50000.0
。
我试过这种方式:
import math
print("Determining the speed to match the Kinetic Energy of the bowling ball dropped in the desert:")
mass = eval(input("Enter the mass of the bowling ball:"))
KE = eval(input("The Kinetic Energy of the ball with a mass of 10 at 100 MPH is:"))
v=0
print("Mass", " ", "Velocity to reach",KE )
for y in range(140, 321, 15):
v = math.sqrt(2*KE*(1//mass))
mass+=15
print (y," ", v)
但继续得到结果:
Mass Velocity to reach 50000.0
140 0.0
155 0.0
我应该在哪里修复以获得结果:
Mass Velocity to reach 50000.0
140 26.72612419124244
155 25.4000254000381
答案 0 :(得分:1)
以下是您应该做的事情:
mass = float(input("Enter the mass of the bowling ball:"))
KE = float(input("The Kinetic Energy of the ball with a mass of 10 at 100 MPH is:"))
v=0
print("Mass", " ", "Velocity to reach",KE )
for y in range(140, 321, 15):
v = math.sqrt((2*KE)/mass)) # no need for * (1/mass), just divide
mass+=15
print (y," ", v)
值得注意的是,您正在+=
上调用增量运算符mass
。因此,一旦您进行了迭代,mass
将不再是10
。因此,再次运行循环会产生不同的结果。
修改强>
从评论中,您似乎希望mass
从140
开始。如果是,则没有理由要求用户mass
为input()
。只需执行以下操作:
KE = float(input("The Kinetic Energy of the ball with a mass of 10 at 100 MPH is:"))
v=0
print("Mass", " ", "Velocity to reach",KE )
for mass in range(140, 321, 15):
v = math.sqrt((2*KE)/mass)) # no need for * (1/mass), just divide
mass += 15
print (mass," ", v)
请注意,如果您希望用户为input()
变量提供mass
,则需要相应地更改循环。