Calling Values

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Aug 3 11:30:41 EDT 2012


> def func1():
> 
>  	num1=10
> 
>  	num2=20
> 
>  	print "The Second Number is:",num2
> 
>  	return
> 
> 
> def func2():
> 
>         func1()
> 	num3=num1+num2
> 
>  	num4=num3+num1
> 
> 	print "New Number One is:",num3
> 
> 	print "New Number Two is:",num4
> 
> 
> This works. Even you can incoportate some conditionals over func1() in func2()
> and run.

This does not work. Python does not get "compiled" in the same manner
as other languages (C, Java etc). Since you never call func2(), there is no
error. Once you try calling func2() you will see it does not work. func1()
does work.

The Second Number is: 20
Traceback (most recent call last):
  File "subha.py", line 24, in <module>
    func2()
  File "subha.py", line 15, in func2
    num3=num1+num2
NameError: global name 'num1' is not defined

> My question can I call its values of func1() too?
> What it is the big deal in experimenting we may come up with some new code or
> a new need?

It is not a big deal, that is how you learn. You are just writing code that
neither works nor really shows enough to tell us why or what you are trying 
to do. Not much I can do to guide or help you because I am completely lost
at your goal. The best I can do at the moment is say. func2 will not work.
You could return num1 and num2 from func1() and then it would work.

def func1():
 	num1=10
 	num2=20
 	print "The Second Number is:",num2
 	return num1, num2


def func2():
       num1, num2 = func1()
	num3=num1+num2
 	num4=num3+num1
	print "New Number One is:",num3
	print "New Number Two is:",num4

    
func2()

Ramit
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list