user6122430
user6122430

Reputation:

Python and Variable Passing

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

Upvotes: 1

Views: 133

Answers (2)

Bahrom
Bahrom

Reputation: 4862

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

Upvotes: 1

Lost
Lost

Reputation: 1008

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)

Upvotes: 1

Related Questions