python中的全局计数器未更新
在上面的代码中,我所要做的就是将全局值递增到无穷大。但我以如下输出结束。我在这个python代码中做错了什么python中的全局计数器未更新,python,python-2.7,Python,Python 2.7,在上面的代码中,我所要做的就是将全局值递增到无穷大。但我以如下输出结束。我在这个python代码中做错了什么 import time counter = 0 def create(): global counter print "The value of counter currently" print counter counter =+ 1 print counter print "The value of counter after in
import time
counter = 0
def create():
global counter
print "The value of counter currently"
print counter
counter =+ 1
print counter
print "The value of counter after increment"
while True:
create()
time.sleep(3)
这是简单的打字错误
The value of counter currently
0
1
The value of counter after increment
The value of counter currently
1
1
The value of counter after increment
The value of counter currently
1
1
The value of counter after increment
(相当于计数器=+1
;将1
重新分配给计数器
)
应替换为
counter =+ 1
这是简单的打字错误
The value of counter currently
0
1
The value of counter after increment
The value of counter currently
1
1
The value of counter after increment
The value of counter currently
1
1
The value of counter after increment
(相当于计数器=+1
;将1
重新分配给计数器
)
应替换为
counter =+ 1
尝试此操作,您需要将
计数器=+1
更改为计数器+=1
counter += 1
尝试此操作,您需要将
计数器=+1
更改为计数器+=1
counter += 1
这阻止了我的一百行代码。该死除了嘲笑我自己,现在我也明白了为什么python甚至没有抛出错误。。非常感谢。@Suhaschikkana,不客气。快乐的python编程。这阻止了我的一百行代码。该死除了嘲笑我自己,现在我也明白了为什么python甚至没有抛出错误。。非常感谢。@Suhaschikkana,不客气。快乐的python编程。