[Tutor] Local vs global

Dave Angel davea at ieee.org
Sat Nov 7 13:26:15 CET 2009


Alan Gauld wrote:
> "bibi midi" <bibsmendez at gmail.com> wrote in
>
>> Although i have read up quite a lot about local and global scope in
>> functions i still 'suffer' understanding it, ... I tried
>> removing it but i get error variable undeclared or something. It is 
>> in the
>> function choose_Cave.
>
> We need to see the exact error text, the whole error.
> Python error messages are very informative and usually
> tell you exactly what is wrong and where (once you get used to them).
>
>> I know that variables inside a function are local in scope and is 
>> gone after
>> the lifetime of the function, i mean after that function is called. 
>> That is
>> why there is the return statement. What i understand you can access 
>> whatever
>> that function did through the return statement.
>
> Specifically you can access the values that the return exposes,
> you cannot access anything else from the function body.
>
>> I tried assigning the function to a variable outside the function 
>> definition
>> but still it doesnt work. Something like:
>>
>> x = choose_Cave()
>> checkCave(x)
>
> That should work
> So what happened? How did the program run and what error did you get?
>
>> location = '/home/bboymen/pyscripts/invent-with-Python/intro.txt'
>> file = open(location)
>> intro = file.read()
>> file.close()
>>
>> def choose_Cave():
>>    global choose
>>    choose = raw_input('choose a cave! (1 or 2): ')
>>    while choose != '1' and choose != '2':  #(a)
>>        print('enter 1 or 2 only.')
>>        choose = raw_input('try again: ')
>>    return choose
>
> You can lose the global line at the top, you are no longer
> modifying the global value you are returning the local one.
>
>> def checkCave(chosenCave):
>>    print('you approach the cave...')
>>    time.sleep(2)
>>    print('it is dark and spooky...')
>>    time.sleep(2)
>>    print('a large dragon jumps over you! he open his jaws and....')
>>    time.sleep(2)
>>    friendlyCave = random.randint(1, 2)
>>    if chosenCave == str(friendlyCave):
>>        print('gives you his treasure!')
>>    else:
>>        print('gobbles you down in one bite!')
>
>> print(intro)
>> choose_Cave()
>
> you are not assigning the return to a value
>
> choose=choose_Cave()
>
>> checkCave(choose)
>> while True:
>>    ask = raw_input('play again? (y/[N])')
>>    reply = ['y', 'ye', 'yea', 'yeah', 'yep', 'yes']
>>    if ask.lower() in reply:
>>        print "\n", intro
>>        choose_Cave()
>>        checkCave(choose)
>>    else:
>>        break
>
> You could tidy that up by moving the first lines inside the loop:
>
>> while True:
>>    print "\n", intro
>>    choose = choose_Cave()
>>    checkCave(choose)
>>    ask = raw_input('play again? (y/[N])')
>>    reply = ['y', 'ye', 'yea', 'yeah', 'yep', 'yes']
>>    if ask.lower() not in reply:
>>          break
>
> HTH,
>
> Alan G.
>
>
Alan - Excellent comments, as usual.

bibimidi - I would point out that after you remove the 'global choose' line as Alan said, you should rename the global variable you're using to store the return value.  They're allowed to have the same name, but it's confusing to a beginner if they do, since you might assume they somehow share the value.

So I'd do something like:

while True:
    ....
    my_cave = choose_Cave()
    checkCave(my_cave)
    ....



Incidentally, as a matter of terminology, you have lots of 'global 
variables' in this code.  location, file, intro, ask and reply are all 
global.  If it were my code, I'd probably move all that logic to one 
more function, call it main().  And the only top-level code would be a 
call to main().

One more important thing.  You used 'file' as a variable name, but it 
already has a meaning in Python, as a built-in type for file 
operations.  No problem in this script, but it's a good practice to 
avoid names in built-ins, as well as names used by stdlib imports  (os, 
sys, math, random, time, ...)

DaveA



More information about the Tutor mailing list