我想創建一個簡單的計數器,所以我創建了這個函式:
count = 0
def add(count):
count = 1
print(count)
如果重新呼叫計數上升到 1,但如果我再次回憶它保持在 1。我還嘗試在呼叫后將計數變數輸出到函式之外,但這導致輸出恒定為 0
uj5u.com熱心網友回復:
在count你的函式變數有不同的范圍以count在腳本中的其余變數。修改前者不影響后者。
您需要執行以下操作:
def add(count):
count = 1
print(count)
return count
count = 0
count = add(count)
count = add(count)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/357998.html
上一篇:這是將x列印n次的正確代碼嗎?
下一篇:所有正偶數的總和
