0

I think I'm just unaware of some basic syntax here.

The following code gives an error "global name 'adder' is not defined".

def math(a, b):
    adder = a + b
    return adder

def usesmath(x, y):
    math(6, 4)
    subtractor = adder - (x + y)
    print subtractor

usesmath(3, 2)

Clearly, this doesn't work because the variable 'adder' does not exist within usesmath(); how can I make the variable be recognised?

5
  • 1
    adder = math(6, 4). This is not basic syntax, it's basic semantics: a function returns a value without a name. Commented May 6, 2014 at 17:45
  • You need to learn what [scope](en.wikipedia.org/wiki/Scope_(computer_science%29) is. Commented May 6, 2014 at 17:45
  • @user3599580 First, do not use tabs in your code, use spaces. It will save you a lot of headache. Commented May 6, 2014 at 17:46
  • Thanks @larsmans! What if math() returned a tuple? Commented May 6, 2014 at 17:47
  • you can use subtractor = math(6, 4) - (x + y) Commented May 6, 2014 at 17:54

1 Answer 1

1

You want this inside usesmath: adder = math(6, 4)

Sign up to request clarification or add additional context in comments.

3 Comments

@Sergio Martinez Will do - it says I have to wait 10 min.
@user3599580 okay sorry for that :)
@user3599580 If math() returned a tuple, adder would get assigned to the tuple. I have a feeling you're trying to get at something specific with that question, but I'm not sure what it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.