[Tutor] thesaurus

Dave Angel davea at ieee.org
Mon Jul 13 01:40:02 CEST 2009


Pete Froslie wrote:
> 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..
>
>   
You don't seem to understand functions.  Part of the point is to *avoid* 
global variables.  If everything a function uses is passed in as 
arguments, and if everything it produces is returned as return value(s), 
it's much easier to see its effect.  And to make sure it doesn't have bugs.
> (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)
>>
>>     
This function doesn't have any effect on global variables.  It returns a 
value that's one more than its argument.  Period.  If you wanted to 
increment word_count using that function, you'd do something like:

word_count = 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
>
>   

I have no clue what this url() function is trying to do.  Nor how you 
expect to use it.  Are you planning to open this file for each word 
contained in it?

DaveA


More information about the Tutor mailing list