Search code examples
pythonfunctionparametersglobal-variables

How to change the global value of a parameter when using a function in python


This is what I want to achieve

Variable=0
Some_function(Variable)
print (Variable)

I want the output to be 1 (or anything else but 0)

I tried using global by defining some_function like this, but it gave me an error "name 'Variable' is parameter and global"

def Some_function(Variable):
    x=Variable+1
    global Variable
    Variable=x


Solution

  • You are using Variable as your global variable and as function parameter.

    Try:

    def Some_function(Var):
        x=Var+1
        global Variable
        Variable=x
    
    Variable=0
    Some_function(Variable)
    print (Variable)