2015-11-21 54 views
0

我一直在外部代碼來源寫我的計算機科學作業,時間多久需要有人猜測一個問題。外部代碼來源作業

然而,當我運行它,我收到此錯誤:

Traceback (most recent call last): 
    File "E:/Documents/Guess band member.py", line 14, in <module> 
    start = time.time() 
AttributeError: 'float' object has no attribute 'time' 

我將不勝感激,如果有人可以幫助我解決這個問題。

import time 
print("You can type 'quit' to exit") 
Exit = False 

Band = ["harry", "niall", "liam", "louis" ] 

while Exit == False: 
    print("Guess a band member") 
    start = time.time() 
    Guess = input(":") 
    end = time.time() 
    Guess = Guess.lower() 
    if Guess == 'quit': 
     Exit = True 
    elif Guess in Band: 
     print("You're right") 
    elif Guess == "zayn": 
     print("Wrong") 
     print("He left.") 
    else: 
     print("Fool!") 
    time = end - start 
    print("You took", time, "seconds to guess.") 
+0

'time = end - start'? – jonrsharpe

回答

3

您更換你在頂部帶有浮動點值這裏進口的time模塊:

time = end - start 

在你while循環的下一次迭代,time現在是一個float對象,而不是模塊對象,所以你的time.time()調用失敗。

重命名變量來的東西,不衝突:

elapsed_time = end - start 
print("You took", elapsed_time, "seconds to guess.") 

作爲一個側面說明,你並不需要使用定點變量(Exit);只需使用while True:和退出循環用break聲明:

​​

在其他情況下,你會使用while not Exit:而不是測試== False