-1
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?

  • 1
    Welcome to Stack Overflow. "Why isn't the s equal to 500?" Why should it be? "Why the function didn't work?" It *did* work. It correctly set the value of the local variable `x` to 500. Why should that have anything to do with `s`? Because you called it like `test(s)`? That has nothing to do with it. Imagine that you had instead written `test(1)`; should 1 become 500? – Karl Knechtel Feb 20 '22 at 07:24

1 Answers1

0

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
Abhyuday Vaish
  • 2,357
  • 5
  • 11
  • 27