我有一个编码项目,要求我以英里/小时为单位计算汽车的加速度。初始速度为0,最终速度为60,该时间应由用户以秒为单位输入。我知道加速度的方程是(最终速度-初始速度)/时间。 但是,我不确定如何将其转换为mph以及如何修改我的方程式。到目前为止,这是我的代码:
time = 0
acceleration = 0
initial_velocity = 0
final_velocity = 60
print('Inputt the time required for the care to reach 60 mph in seconds.')
time = float(input())
acceleration = (final_velocity - initial_velocity)/time
print('The acceleration of the car is:' acceleration')
谢谢!
答案 0 :(得分:0)
根据python doc,type()
将用户输入转换为字符串,因此需要首先将其转换为数字类型。
此外,要获得真实结果,您必须先将inputs
转换为小时或将time
转换为mps。
要实现第一个选项,您可以添加:
final_velocity
答案 1 :(得分:0)
python输入采用字符串形式的输入,因此您必须将其转换为浮点数或至少
time = float(input())
time = int(input())
最后一行还有两个错误
print('The acceleration of the car is:', acceleration)
答案 2 :(得分:-1)
在Python 3中,input()
function always returns a string。
您需要将其解析为一个数字-在您的情况下,您需要用float()
解析的浮点数:
time = float(input())