2017-09-24 164 views
-5
while count > 0: 
    if count = 0: 
     return n 
    elif count < 0: 
     print(" ") # prints empty if n is below 0 
    else: 
     count = count - 1 
     collect += math.ceil((n - 5)/2) 
    return collect 

輸入是(1003,3) - 結果是499,這意味着它只是循環一次,然後減去5,然後除以2,然後停止。有人知道爲什麼爲什麼只循環一次?

+6

'='是不是''==。 – Chris

+0

仍然不起作用 – Gekz

+3

您也未顯示完整的代碼。輸入_to what_?來自什麼?你真的想在你的循環中「返回」嗎? – Chris

回答

1

你的內部條件與時間無關。你在循環中有一個return語句,所以是的,它只循環一次。

開始與這個

import math 

n, count = (1003, 3) 
print("N = " + str(n)) 
while count > 0: 
    n = math.ceil((n - 5)/2) # Update this to do whatever your logic is 
    print(count, n) 
    count -= 1 
if n < 0: 
    print("N is negative") 
else: 
    print("N = " + str(n)) 
0

你有幾個問題。

首先,您的語法沒有均勻縮進。

其次,您的if語句有=而不是==。第一個用於賦值給變量,第二個用於檢查相等性。

第三,你有一個return語句,它將從這個循環內部的任何函數中退出。

相關問題