2014-11-02 162 views
-1

我在程序中兩次使用此語句。第二次失敗。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循環之外,但裏面沒有它。已經完成之後是否需要轉換爲浮動狀態?

+6

如果來自用戶的輸入不能轉換爲「浮點數」,它將不起作用。這取決於用戶輸入的內容。 – khelwood 2014-11-02 18:38:20

+1

我們可以有更多的上下文嗎? – phantom 2014-11-02 18:51:59

+1

您給出的輸入是什麼?請記住,您只能在一行中輸入一個浮點數。 – GingerPlusPlus 2014-11-02 19:11:52

回答

0

做這種方式,嘗試和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已同時

+3

是的,沒有。這完全不起作用。 – 2014-11-02 18:41:24

+0

爲什麼這不起作用? – Hackaholic 2014-11-02 18:42:43

+1

'pitchertime'將永遠是一個字符串... – phantom 2014-11-02 18:43:07

1

的原因,這不幾乎可以肯定的工作無關,與之前的一些價值您的while循環。在沒有包含代碼的情況下,這很奇怪,它最可能失敗的原因是用戶提供的輸入不能轉換爲float。 (例如,如果他們在你的input輸入1.0fzjfk,在這種情況下,與float()你實際上調用float("1.0fzjfk"),這是不可能的。)

你的問題的實質內容幾乎完全建立在用戶輸入的,雖然如此,它的很難指出你的目的究竟在哪裏以及如何失敗。

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循環內是不會改變的錯誤。不完全確定你的意思是考慮到你沒有給出太多的背景......

希望這會有所幫助,祝你好運!

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

以前,你說

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

有差別。

inputstdin讀取一行,並將其作爲字符串返回。第一種情況下,您在pitcherTime中存儲的結果(字符串)。

在第二種情況下,您編寫提示符,獲取字符串,然後嘗試將其轉換爲float
此時發生錯誤。蟒蛇說:究竟出了什麼問題:

could not convert string to float: 

As furkle said,它只是意味着你給字符串不能轉換爲float

所以,這個問題不在你的代碼中。問題在於你或任何人對該計劃的投入。

相關問題