我试图编写代码来显示从计算中得出一个高的球
但是无论我怎么键入,它总是在高度变量上说“ 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'
为什么代码不想转换?
答案 0 :(得分:0)
之所以会这样,是因为时间是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)