1

I have spent the last few hours reading around on the net and looking at the Python online manual (currently using Python v2.7) but I'm still struggling to understand how I pass a variable from one function to another.

At the moment I am just starting out with something simple, to get my head around passing variables. I have 3 functions (one which will display a hello message), another that will ask for user input and the third that will take the user input and perform the calculation).

For the life of me I cant figure how to get the user input values into the calculation function. I thought that once I entered the values in the user input, I could send them to the calc function. But I cant.......I keep getting an error message: calc = calculation(num1, num2) NameError: global name 'num1' is not defined

Can someone point out where I am going wrong ? This is driving me nuts !

def main():
    message()
    input = user()
    calc = cal(num1, num2)

def message():
    print "Welcome message"

def user():
    side_a = int(raw_input("Enter a: "))
    side_b = int(raw_input("Enter b: "))
    return side_a, side_b

def cal(num1, num2):
    side_c = side_a + side_b
    return side_c
1
  • 1
    You are sending the function num1 and num2 without defining what num1 and num2 are. You need to have num1 = some number and num2 = some number before you can call cal(num1, num2) Commented Mar 28, 2016 at 2:59

2 Answers 2

1

So think of it like this:

You have a function (def user() ) which will return two values. You have to receive those two values and store them somewhere. In your main() you use variable "input" which is now have to hold two values as written. If you were to do:

print input

You'd get (value of side A, value of side B) passed back to you. If you instead wrote

print input[0]

It would print just side A. More easy (in my mind) would be having two variables standing by to receive the returned values:

input1, input2 = user()

Now in your def cal(num1,num2) it is expecting to receive two arguments and return one value. So your main should look like this:

def main()
   message()
   input = user()
   calc = cal(input[0], input[1])

When you define your function, the variables you use (num1, num2) is how you are going to represent the inputs in that function. If you just pass the arguments num1, num2 they have no value as they were not declared in main. The names do not have to match, you just have to pass the right number(and type) of arguments.

So for example:

def sample(int1, int2, string):
    number3 = number1 + number2
    print(string1 + str(number3))

You could pass this raw data, or pass it variables. In this sample main, the printed output would be the same:

def main():
    number1 = 2
    number2 = 2
    string1 = "two plus two equals: "
    sample(2,2,"two plus two equals: ")
    sample(number1,number2,string1)
Sign up to request clarification or add additional context in comments.

Comments

1

Change what you have to

def main():
    message()
    num1, num2 = user()  # grab the numbers from user fn and store them in num1, num2
    calc = cal(num1, num2)

def message():
    print "Welcome message"

def user():
    side_a = int(raw_input("Enter a: "))
    side_b = int(raw_input("Enter b: "))
    return side_a, side_b

def cal(side_a, side_b):
    side_c = side_a + side_b
    return side_c

2 Comments

you need to change your variable names in cal(). you're using the names declared in user().
Thank you Tom and rmunn, fixed the issue. Sorry had just copy pasted the code and worked based off that.

Your Answer

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