ValueError:无法将字符串转换为浮点数:
我在程序中两次使用此语句。第二次失败。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
循环之外,但里面没有它。已经完成之后是否需要转换为浮动状态?
做这种方式,尝试和CATH异常提高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已同时
是的,没有。这完全不起作用。 – 2014-11-02 18:41:24
为什么这不起作用? – Hackaholic 2014-11-02 18:42:43
'pitchertime'将永远是一个字符串... – phantom 2014-11-02 18:43:07
的原因,这不几乎可以肯定的工作无关,与之前的一些价值您的while
循环。在没有包含代码的情况下,这很奇怪,它最可能失败的原因是用户提供的输入不能转换为float
。 (例如,如果他们在你的input
输入1.0fzjfk
,在这种情况下,与float()
你实际上调用float("1.0fzjfk")
,这是不可能的。)
你的问题的实质内容几乎完全建立在用户输入的,虽然如此,它的很难指出你的目的究竟在哪里以及如何失败。
当用户键入一个不能转换为浮点数的值时,会发生这种情况。您可以检测,如果这种情况发生在一个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循环内是不会改变的错误。不完全确定你的意思是考虑到你没有给出太多的背景......
希望这会有所帮助,祝你好运!
泰这种方法
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()
以前,你说
I use this statement twice in my program. The second time it fails.
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
。
此时发生错误。蟒蛇说:究竟出了什么问题:
could not convert string to float:
As furkle said,它只是意味着你给字符串不能转换为float
。
所以,这个问题不在你的代码中。问题在于你或任何人对该计划的投入。
如果来自用户的输入不能转换为“浮点数”,它将不起作用。这取决于用户输入的内容。 – khelwood 2014-11-02 18:38:20
我们可以有更多的上下文吗? – phantom 2014-11-02 18:51:59
您给出的输入是什么?请记住,您只能在一行中输入一个浮点数。 – GingerPlusPlus 2014-11-02 19:11:52