0

I'm trying to create a function in Python 3 that can edit multiple variables, for example

    x = 1
    y = 2
    def addOne(x1):
        x1 = x1 + 1
    test(y)
    print(y)

is there any way that I could make this work so that addOne can edit any variable put in to this?

1 Answers1

0

If the variable are global you could do it like this

x = 1
y = 2

def addOne(x1):
    globals()[x1] = globals()[x1] + 1

addOne('y')
print(y)
3
saurabh baid
  • 1,819
  • 1
  • 14
  • 26