How to change a variable after it is already defined?

前端 未结 6 1894
一整个雨季
一整个雨季 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:24

    Your error has nothing to do with is being already defined… A variable is only valid inside it's so called scope: If you create a variable in a function it is only defined in this function.

    def test():
       x=17
       print(x) # returns 17
    
    test()
    print(x) # results in an error.
    

提交回复
热议问题