so, I've been playing with the functions a bit-- took a little time to get the hang of them after I started that other mess. Initially, I've done the opposite suggested here by creating functions without the input values. I do see the where it will pay off in re-usability that route.. Alan, I'll read your tutorial on modules and functions.
Things that are arising for me now: (1)the use of global variables- seems it would be easier to communicate between functions with 'I/O' values if I understood where the best location for declaration is.. (2)not sure why this function doesn't work: word_count = 0 #set up variable to increment through text def increment(x): > return x+1 > > increment(word_count) > (3) related to strings: Trying a function that will strip unwanted words off of the url before I bounce it off the website. For instance, I don't need to search for a, was, is... This is working, but with strange results-- often it will find an 'a' in the middle of a word and replace it with text and such. using rstrip, but with varying results: #this is the url as a string > http://words.bighugelabs.com/api/2/e413f24701aa30b7d441ca43a64317be/A/ > > thesaurus = string.rstrip(url(),"/A/") + '/' # stripping the end off and > re-adding the '/' > The url function btw: def url(): > fin = open("journey_test.txt", "r") > response = re.split(r"[/|/,\n, , ,:\"\"\.?,)(\-\<>\[\]'\r']", > fin.read()) > thesaurus = API_URL + response[word_number] + '/' #API_URL is > established at the start of the code > return thesaurus > Pete F On Sun, Jul 12, 2009 at 4:00 AM, Alan Gauld <[email protected]>wrote: > > "Pete Froslie" <[email protected]> wrote > > This seems the basic form for a function: >> >> *def** hello*(): >> >>> *print* "Hello World!" >>> *return* >>> >> > That is a very limited form that encourages bad practice. The more > general form is: > > def aFunction(inputValue1, inputValue2,...): > # do some processing here > return output_value1, outputValue2.... > > By adding the input and output values we make the function > independant of its immediate surroundings and therefore > more easily reusable across different programs. > > We can then call it like: > > x,y = aFunction(a,b) > > I assume each part that can be separated will be done so in this format at >>> >> the start of the code; consequently, allowing me to call 'hello' later >> when >> I need it. Does this also mean that I will be able to call those functions >> separately later when I import 'thesaurus.py' into a new code also? >> > > Exactly. The first benefit of functions is in encapsulating parts of your > program and providing structure which makles it easier to read but > the real power of functions is in making those blocks of code available > to other programs too. > > Read my tutorial topic on Moduules and Functions more information. > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - [email protected] > http://mail.python.org/mailman/listinfo/tutor > -- Pete Froslie 617.314.0957 http://www.froslie.net
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
