我试图编写代码来显示一个球从计算中升多高但无论我怎么输入它,它一直说它在高度变量上的“cannot convert string to float
”
time = input("How long did the ball go up? in seconds\n")
time = float(time)
velocity = input("What was the initial velocity?\n")
velocity = float(velocity)
height = ("What was the initial height?\n")
height = float(height)
answer = (time ** 2 * -16) + (velocity * time) + height
print(answer)
ValueError:无法将字符串转换为float:'初始高度是多少?\ n'
为什么代码不想转换?
这是因为时间是一个string
。您无法更改数据类型,但可以进行转换。你应该做的是:
time = input("How long did the ball go up? in seconds\n")
velocity = input("What was the initial velocity?\n")
height = input("What was the initial height?\n")
answer = (float(time) ** 2 * -16) + (float(velocity) * float(time)) + float(height)
print(answer)