Why not try passing the variable to a modifying function:
x = ''
def xy(x):
x += "String"
return x
x = xy(x)
print(x)
This defines a function that takes an input x
and then returns it after modifying it. This modified value is then reassigned to the x
that is outside of scope. Perhaps a more clear example would look like
x = 'My input' # create an initial variable
def xy(my_input):
my_input += " String" # append another string to our input string
return my_input # give the modified value back so we can overwrite old value
x = xy(x) # reassign the returned value of the function to overwrite this variable
print(x)
Outputs:
My input String
Hopefully this illustrates that a local function can modify a value if it is input to the function. This modified value should then be returned and used to overwrite the old value. This technique not only allows you to pass global variables to other functions for modification, but allows local variables to be passed to functions for modification as well.