简体   繁体   中英

Global function code does not work in Python

This is the code on the book I`m reading:

>>> var=99
>>> def funct1():
    global var
    var+=1

>>> var
100

but when I try to run this code on my pc this happens:

>>> var=99
>>> def funct1():
    global var
    var+=1

>>> var
99

Does anyone know why this happens? My book is about Python 3.3 and after and I`m using Python version 3.8.3 so it should be ok...

I am not sure about the context of the book. But you should first call the function for var to change.

Like this:

var=99
>>> def funct1():
    global var
    var+=1

>>> funct1()
>>> var
100

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM