Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的全局计数器未更新_Python_Python 2.7 - Fatal编程技术网

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

在上面的代码中,我所要做的就是将全局值递增到无穷大。但我以如下输出结束。我在这个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 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编程。