我想創建一個簡單的計數器,所以我創建了這個函數:
count = 0
def add(count):
count += 1
print(count)
如果重新調用計數上升到 1,但如果我再次回憶它保持在 1。我還嘗試在調用後將計數變量輸出到函數之外,但這導致輸出恆定為 0
在count
你的函數變量有不同的範圍以count
在腳本中的其餘變量。修改前者不影響後者。
您需要執行以下操作:
def add(count):
count += 1
print(count)
return count
count = 0
count = add(count)
count = add(count)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句