无法在 Python 中添加多行 input() 语句而无需 IDLE 要求提示

问题描述 投票:0回答:1

我是一般编程新手,我似乎无法让编辑器窗口让我添加多个输入命令

每当我尝试添加它们时,它只会显示上一个输入命令的结果,并且不允许我添加更多内容来完成我的代码

distance = 450 
speed = eval (input("Enter Average Speed:"))
#after pressing enter here, the IDLE shows the result of this line but when I type in the result it forgets the variable speed and I have no option left 

time = distance/speed 
print ("Distance is:", distance)
print ("Time is:" , time , "hr")```

python input python-3.10
1个回答
0
投票

我认为您正在寻找的函数是 float() 而不是 eval()。 Eval 本质上接受一个字符串并运行其内容,就好像它是它自己的 python 代码一样。如果您只想将输入转换为数字,那么 float 会产生奇迹。

© www.soinside.com 2019 - 2024. All rights reserved.