2017-07-07 106 views
-2
import time as t 
t = t.time 
starttime = t 
def calcprod(): 
    prod = 1 
    for i in range(1, 1000000): 
     prod = prod * i 
p = calcprod() 
print("The result is %s digits" % (str(len(p)))) 
endtime = t 
print("It took %s seconds to calculate" % (starttime - endtime)) 

我不知道如何修復我的代碼。爲什麼我不斷收到一個錯誤,說endtime中的e是Python中的無效語法?

+2

是在一開始的時候,你貼一個錯字反引號StackOverflow的源代碼還是實際上隱藏在源代碼上的?除此之外,我沒有看到任何語法錯誤,您得到的確切'無效語法'報告是什麼(從REPL或IDE,...)? –

+0

是的,Stackflow似乎在我的問題和代碼的開始和結尾處反覆出現。我不知道爲什麼。 – Codebek

+1

這裏沒有SyntaxError。現在有什麼是1)由於'p'是'None',所以調用'len(p)'時出錯「(即使你返回了'prod',你不會從'calcprod'函數返回任何東西,它是' int'沒有len)和2)執行'starttime - endtime'時出錯,因爲你將這兩個函數分配給了*函數* time.time和* not *作爲*調用的結果*函數'時間'(即't()') –

回答

0

下面你可以看到一些希望有用的意見,以解決你的問題:

import time as t 
t = t.time # Don't name this the same as your time library 
       # Also use t.time() instead of t.time 
starttime = t 
def calcprod(): 
    prod = 1 
    for i in range(1, 1000000): 
     prod = prod * i 
     # return a value here (hint: cast the result to a string) 
p = calcprod() 
print("The result is %s digits" % (str(len(p)))) 
endtime = t # This assigns the same value to 'endtime' as starttime. 
       # You need to measure the current time again. 
       # How can you do this? 
print("It took %s seconds to calculate" % (starttime - endtime)) 
       # Does it make sense to calculate starttime-endtime? 
       # Which one is the bigger value? 
0

你忘了return語句在函數也許這就是問題所在

相關問題