'Why doesn't the variable's value change? [duplicate]

a = 10
b = 5
s = a * b

def test(x):
  x = a * a * b

test(s)

print(s)

Why isn't the s equal to 500? Why the function didn't work?



Solution 1:[1]

This is because you are printing the variable s not x. To change the value of s to 500:

a = 10
b = 5
s = a * b

def test(s):
  s = s * a # The value of s will be 500
  print(s) #This will print 500. 

test(s) # Here you are passing the value of `s` which is 50 to the test function

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1