-1
hi = 1
result = 0
def hello(x):
    x = x + 10
    result = x
helli(hi)
print(result)

Why does my code Output "0" and not 11?

Jibril
  • 23
  • 3

1 Answers1

2

You need to use the keyword global for that. Then edited values inside the function actually impact the variable from outside the function. This is not the recommended approach.

hi = 1
result = 0

def hello(x):
    global result
    x = x + 10
    result = x

hello(hi)
print(result)

The recommended approach is, however, to return such value:

def hello(x):
    return x + 10

hi = 1
result = 0
result = hello(hi)
print(result)
miquelvir
  • 1,748
  • 1
  • 7
  • 21