ValueError:无法将字符串转换为float:

时间:2014-11-02 18:36:17

标签: python

我在程序中两次使用此语句。第二次失败。

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 个答案:

答案 0 :(得分:1)

这几乎无效的原因与你的while循环无关。如果没有未包含的代码执行某些非常奇怪的操作,那么最有可能失败的原因是用户提供的输入无法转换为float。 (例如,如果他们在您的1.0fzjfk中输入了input,在这种情况下float()您实际上正在调用float("1.0fzjfk"),这是不可能的。)

然而,问题的实质几乎完全取决于用户输入,因此很难确切地说明问题的确切位置和方式。

答案 1 :(得分:0)

这样做,试试并引发异常引发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在

之前有一些价值

答案 2 :(得分:0)

当用户输入浮点数不可接受的值时,就会发生这种情况。您可以通过将其包裹在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.

但实际上,仅仅将其置于while循环中并不会改变错误。考虑到你没有提供太多背景,不完全确定你的意思......

希望这有所帮助,祝你好运!

答案 3 :(得分:0)

这种方法

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()

答案 4 :(得分:0)

以前你说过

  

我在程序中两次使用此语句。第二次失败。

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: "))

有区别。

inputstdin读取一行并将其作为字符串返回。在第一种情况下,您在pitcherTime中存储的结果(字符串)。

在第二种情况下,您编写提示,获取字符串,然后尝试将其转换为float
目前发生错误。 Python说完全出了什么问题:

could not convert string to float: 

As furkle said,只是表示您提供的字符串无法转换为float

所以,问题不在你的代码中。问题在于您或任何人对该程序的输入。