Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:无法将字符串转换为浮点:_Python - Fatal编程技术网

Python ValueError:无法将字符串转换为浮点:

Python ValueError:无法将字符串转换为浮点:,python,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=pi

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

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: "))
收到错误:

pitcherTime=float(input("Enter time for " +str(pitcherName) +" in milliseconds: "))
ValueError: could not convert string to float: 

我知道这可能是一个基本问题,但我想知道为什么它在
while
循环之外工作,而不是在循环内部工作。在已经完成后是否不需要转换为float

这样做,尝试并处理异常raise VALUERROR

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
。(例如,如果他们在您的
输入中键入
1.0fzjfk
,在这种情况下,使用
float()
实际上是在调用
float(“1.0fzjfk”)
,这是不可能的。)


不过,问题的实质几乎完全取决于用户输入,因此很难准确指出问题在哪里以及如何失败。

当用户输入的值不能转换为浮点值时,就会出现这种情况。您可以通过将其包装在一个
try…中来检测是否发生这种情况,但以下情况除外:

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循环并不能改变错误。考虑到你没有给出太多的上下文,我不完全确定你的意思

希望这有帮助,祝你好运

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()
你之前说过

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

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: "))
如果你没有更改代码,那就不是真的

# 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
中读取一行,并将其作为字符串返回。在第一种情况下,存储在
pitchrtime
中的结果(字符串)

在第二种情况下,您编写一个提示符,获取字符串,然后尝试将其转换为
float

在错误发生的那一刻。Python确切地说是出了什么问题:

could not convert string to float: 
,它只是意味着您提供的字符串不能转换为
float


因此,问题不在代码中。问题在于您或任何人向程序提供的输入。

如果用户的输入不能转换为
浮点值,则无法工作。这取决于用户输入的内容。我们可以有更多的上下文吗?您提供了什么作为输入?记住,一行中只能有一个浮点数。