ValueError: 无法将字符串转换为浮点数:
我在我的程序里用了这个语句两次。第二次的时候出错了。
output=""
pitcherName=input("Enter name of the next contestant, or nothing to quit: ")
pitcherTime=input("Enter time for " +str(pitcherName)+ " in milliseconds: ")
highestSpeed=pitcherTime
lowestSpeed=pitcherTime
fastestPitcher=pitcherName
slowestPitcher=pitcherName
while pitcherName!="":
pitcherName=input("Enter name of the next contestant, or nothing to quit: ")
pitcherTime=float(input("Enter time for " +str(pitcherName) +" in milliseconds: "))
pitcherSpeed=round(40908/pitcherTime, 2)
output=output +str(pitcherName)+ "\t" +str(round(pitcherTime, 2)) + "\t" +str(round(pitcherSpeed, 2)) + "\n"
if fastestPitcher==pitcherName and pitcherSpeed>highestSpeed:
fastestPitcher=pitcherName
highestSpeed=pitcherSpeed
elif slowestPitcher==pitcherName and pitcherSpeed>lowestSpeed:
slowestPitcher=pitcherName
lowestSpeed=pitcherSpeed
print("Name" + "\t" +"Time" +"\t" +"Speed" + "\n" + "===========================" + "\n")
print(output)
print("Slowest pitcher was " +str(slowestPitcher) +" at " +str(round(lowestSpeed, 2)) +" miles per hour")
print("Fastest pitcher was " +str(fastestPitcher) +" at " +str(round(highestSpeed, 2)) +" miles per hour")
exit=input("Press nothing to`enter code here` exit")
收到的错误信息:
pitcherTime=float(input("Enter time for " +str(pitcherName) +" in milliseconds: "))
ValueError: could not convert string to float:
我知道这可能是个基础问题,但我想知道为什么在while
循环外面能正常工作,而在里面却不行。难道在已经转换成浮点数之后就不需要再转换一次了吗?
5 个回答
之前你说过
我在程序中用这个语句两次。第二次它失败了。
pitcherTime=float(input("Enter time for " +str(pitcherName) +" in milliseconds: "))
如果你没有改动代码,那就不是真的。
# case 1
pitcherTime=input("Enter time for " +str(pitcherName)+ " in milliseconds: ")
#later...
#case 2
pitcherTime=float(input("Enter time for " +str(pitcherName) +" in milliseconds: "))
这里有个区别。
input
是从 stdin
读取一行内容,并把它作为字符串返回。你在第一次的情况下把这个结果(字符串)存储在 pitcherTime
里。
在第二种情况下,你先写了一个提示,获取字符串,然后尝试把它转换成 float
。
错误发生的时刻,Python 会准确告诉你哪里出错了:
could not convert string to float:
正如 furkle 所说,这简单意味着你给了一个无法转换成 float
的字符串。
所以,问题不在你的代码里。问题在于你或者其他人输入给程序的内容。
当用户输入一个无法转换成浮点数的值时,就会发生这种情况。你可以通过把它放在一个 try...except
结构里来检测这个问题,像这样:
try:
pitcherTime=float(input("Enter time for " +str(pitcherName) +" in milliseconds: "))
except ValueError:
continue # Start the loop over if they enter an invalid value.
不过,单单把这个放在一个循环里并不能解决错误。我不太明白你说的这个意思,因为你没有提供太多背景信息。
试试这个方法
def get_time():
pitcherName = input("Enter name of the next contestant, or nothing to quit: ")
goodinput = False
while not goodinput:
try:
pitcherTime = float(input("Enter time for " + str(pitcherName) + " in milliseconds: "))
goodinput = True
except ValueError:
goodinput = False
print("Invalid Input")
get_time()
这样做,试着捕捉引发的异常 ValueError
while pitcherName!="":
try:
pitcherName=input("Enter name of the next contestant, or nothing to quit: ")
pitcherTime=float(input("Enter time for " +str(pitcherName) +" in milliseconds: "))
except ValueError:
print "input error"
这里假设 pitcherName 在进入 while 循环之前已经有了一些值
这段代码没有正常工作,几乎肯定和你的 while
循环没有关系。除非有其他代码在做一些很奇怪的事情,否则最可能的原因是用户输入的数据无法转换成 float
类型。比如说,如果用户在你的 input
中输入了 1.0fzjfk
,那么你调用 float()
时实际上是在尝试执行 float("1.0fzjfk")
,这是不可能成功的。
你的问题主要是因为用户输入的内容,所以很难准确指出问题出在哪里,以及是怎么出错的。