How to change a variable after it is already defined?

前端 未结 6 1884
一整个雨季
一整个雨季 2020-11-22 07:03

I\'m trying to add or subtract from a defined variable, but I can\'t figure out how to overwrite the old value with the new one.

a = 15

def test():
    a =          


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 07:10

    You're modifying a variable a created in the scope of the function test(). If you want the outter a to be modified you could do:

    a = 15
    
    def test():
        global a
        a = a + 1
        print(a)
    
    test()
    

提交回复
热议问题