Zoklet.net

Go Back   Zoklet.net > Technology > Technophiles and Technophiliacs > Codes of all kinds

Reply
 
Thread Tools
  #1  
Old 04-28-2009, 05:49 AM
D̉̋ilẻ̡̓t͓tant͖͑̓e D̉̋ilẻ̡̓t͓tant͖͑̓e is offline
Regular
 
Join Date: Jan 2009
Thanks: 21
Thanked 13 Times in 12 Posts
Default [ASK-PYTHON]Some problem with parameter

So, I recently learn pyhon and stuck at defining parameters
I mean the variables that defined in the parameters can't work in others

Here's an example:

Code:
def add(a,b):
	print a,"+",b,"=",a+b

def inputnum():
	a=input("First number: ")
	b=input("Second number: ")

inputnum()
add(a,b)
Any help will be appreciated
Reply With Quote
  #2  
Old 04-28-2009, 05:20 PM
MadVillain MadVillain is offline
Member
 
Join Date: Mar 2009
Thanks: 0
Thanked 8 Times in 5 Posts
Default Re: [ASK-PYTHON]Some problem with parameter

Variables have what's called scope. When variables are defined in the context of a function like you've done, they have no meaning outside of that function. You can however define these as global variables which exist outside of either function where they are visible to both functions and any future functions you make. You can fix your code like this but don't get used to using global variables often:

Code:
def add(a,b):
	print a,"+",b,"=",a+b

def inputnum():
	global a
	global b
	a=input("First number: ")
	b=input("Second number: ")

inputnum()
add(a,b)

Last edited by MadVillain; 04-28-2009 at 05:24 PM.
Reply With Quote
The following users say "It is so good to hear it!":
D̉̋ilẻ̡̓t͓tant͖͑̓e (04-29-2009), Rich White Male (05-18-2009)
  #3  
Old 04-29-2009, 12:49 AM
D̉̋ilẻ̡̓t͓tant͖͑̓e D̉̋ilẻ̡̓t͓tant͖͑̓e is offline
Regular
 
Join Date: Jan 2009
Thanks: 21
Thanked 13 Times in 12 Posts
Default Re: [ASK-PYTHON]Some problem with parameter

Thank you so much
I almost manually put the input in every other function

But why I shouldn't get used to using global variables?
Reply With Quote
  #4  
Old 04-29-2009, 04:35 PM
MadVillain MadVillain is offline
Member
 
Join Date: Mar 2009
Thanks: 0
Thanked 8 Times in 5 Posts
Default Re: [ASK-PYTHON]Some problem with parameter

Global variables are a lot harder to keep track of. For instance you could run one function that is expecting a global variable to be consistent and if another function happens to change that variable it will fuck up the result from the other function. This is pretty trivial in your code, there's not much to keep track of, but in large systems where no one person actually knows everything going on in the code, you want to avoid global variables.
Reply With Quote
  #5  
Old 05-05-2009, 02:22 PM
Cir Cir is offline
Member
 
Join Date: Feb 2009
Location: بيرث
Thanks: 19
Thanked 76 Times in 46 Posts
Default Re: [ASK-PYTHON]Some problem with parameter

I'm just learning myself, so feel free to berate me if I've done something wrong, but this code avoids using global variables, it also uses raw_input instead of input which makes error handling easier, and it of course turns the strings a and b into integers before it adds them. Otherwise if A is 3 and B is 4, it will print the string assigned to a, and the string assigned to b, one after the other, which is 34. You do get errors if you enter something other than an integer, but that's easily avoided.

Code:
a = 0
b = 0
def add(a,b):
	print a,"+",b,"=",a+b

def inputnum(a, b):
	a=raw_input("First number: ")
	b=raw_input("Second number: ")
	return a, b
    
a, b = inputnum(a,b)
a = int(a)
b = int(b)
add(a,b)
Reply With Quote
  #6  
Old 05-18-2009, 04:24 PM
Rich White Male Rich White Male is offline
seeks filthy whore
 
Join Date: Jan 2009
Location: America
Thanks: 81
Thanked 109 Times in 66 Posts
Default Re: [ASK-PYTHON]Some problem with parameter

I was wondering the same thing myself...
Reply With Quote
Reply

Bookmarks

Tags
askpythonsome, parameter, problem

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
lye problem catman2000 Flasks and Beakers 18 03-17-2009 02:02 AM
Need help with some problem. Skon93 Bad Ideas 4 03-15-2009 03:04 AM
i have problem Earlmeyerthebuttpirate Bat Country 4 03-13-2009 10:17 AM
i have a problem Earlmeyerthebuttpirate Bat Country 7 02-15-2009 08:50 AM
Archived: Function for searching a list of objects in Python Animal Farm Pig Codes of all kinds 0 02-06-2009 07:23 PM


All times are GMT. The time now is 08:36 PM.


Hot Topics
On IRC
Users: 4
Messages/minute: 0
Topic: "http://www.zoklet.net/..."
Users: 24
Messages/minute: 1
Topic: "dangly parts"
Users: 11
Messages/minute: 0
Topic: "vaginaboob"
Advertisements
Your ad could go right HERE! Contact us!

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.