[Tutor] Local vs global

Alan Gauld alan.gauld at btinternet.com
Sat Nov 7 10:16:37 CET 2009


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




More information about the Tutor mailing list