Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #26440

RE: Calling Values

From "Prasad, Ramit" <ramit.prasad@jpmorgan.com>
Subject RE: Calling Values
Date 2012-08-03 15:30 +0000
References <8bdc29d5-fa88-4ead-a4a1-135d708eeb57@googlegroups.com> <21f0980d-10dc-4970-bace-8e909994fafd@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2908.1344007857.4697.python-list@python.org> (permalink)

Show all headers | View raw


> 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.  

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Calling Values Subhabrata <subhabangalore@gmail.com> - 2012-08-03 04:49 -0700
  Re: Calling Values Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-08-03 13:07 +0100
  Re: Calling Values Nobody <nobody@nowhere.com> - 2012-08-03 13:44 +0100
  Re: Calling Values Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-08-03 14:32 +0200
  Re: Calling Values subhabangalore@gmail.com - 2012-08-03 07:38 -0700
    RE: Calling Values "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-08-03 15:30 +0000
    Re: Calling Values Ethan Furman <ethan@stoneleaf.us> - 2012-08-03 08:37 -0700
  Re: Calling Values Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-03 16:24 +0000
  Re: Calling Values Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-08-03 13:20 -0400
    Re: Calling Values subhabangalore@gmail.com - 2012-08-03 11:23 -0700
    Re: Calling Values subhabangalore@gmail.com - 2012-08-03 11:23 -0700
  Re: Calling Values alex23 <wuwei23@gmail.com> - 2012-08-05 19:56 -0700

csiph-web