2017-04-24 141 views
0

更新:我已經通過在循環開始時將階乘重置爲1並消除不必要的代碼來解決了複合階乘問題。這是新版本。我如何得到一個計算階乘的while循環來忽略以前的階乘因子?

more = "yes" 
while more == "yes": 
    n = int(input("Enter a number to get its factorial: ")) 
    if n >= 0: 
     factorial = 1 
     for num in range(1, n+1): 
      factorial *= num 
     print(n,'!=', factorial) 
     more = input("Would you like to get another number's factorial?:") 
    else: 
     print("Factorials are not defined for negative integers.") 
print("Thank You!") 
+0

更新:我修復了問題並簡化了代碼。 – AndroidFan253

回答

1

您需要在while循環開始時重置因子變量爲1。

另外,您需要檢查n是否爲每個輸入的正數,因爲它目前只對第一個輸入有效。

+0

謝謝迪馬!我解決了複合問題,但現在我遇到了一個問題,它顯示輸入的負整數等於一個階乘,然後要求另一個整數。這是我的... – AndroidFan253

+0

代碼可以很簡單,這將幫助你找出問題。對於初學者來說,將所有內容都放在while循環中,並且只在代碼中有一次要求編號的代碼。 – Dima

+0

我非常感謝迪馬解決了這個問題!你最好! – AndroidFan253