我在程序中兩次使用此語句。第二次失敗。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
循環之外,但裏面沒有它。已經完成之後是否需要轉換爲浮動狀態?
如果來自用戶的輸入不能轉換爲「浮點數」,它將不起作用。這取決於用戶輸入的內容。 – khelwood 2014-11-02 18:38:20
我們可以有更多的上下文嗎? – phantom 2014-11-02 18:51:59
您給出的輸入是什麼?請記住,您只能在一行中輸入一個浮點數。 – GingerPlusPlus 2014-11-02 19:11:52