2017-03-03 86 views
0

我正在嘗試使用for循環和累加器來計算的階乘n。我遇到了範圍命令及其兩個參數 - 開始和結束的問題。我得到無效的語法錯誤。代碼如下:使用兩個參數計算n的階乘使用範圍

# factorial.py 
# Program to compute the factorial of a number 
# Illustrates for loop with an accumulator 

def main(): 

    n = int(input("Please enter a whole number: ")) 

    fact = 1 

    for factor in range(1, (n + 1)) 
     fact = fact * factor 

    print("The factorial of", n, "is", fact) 


main() 

問題在哪?

我正在使用Python 3.6。

+1

添加一個冒號。範圍(1,(n + 1)) - >'範圍(1,(n + 1)):' – Ben

+0

OMG。非常感謝你。 – wraith46

回答

0

你簡單地在你的範圍函數後面忘了:,因爲它是一個for循環^。^

+0

確實。謝謝。 – wraith46