[Tutor] Local vs global

Dave Angel davea at ieee.org
Sun Nov 8 14:38:01 CET 2009


bibi midi wrote:
> On Sun, Nov 8, 2009 at 6:20 AM, bibi midi <bibsmendez at gmail.com> wrote:
>
>
>   
>> Thank you all for the helpful insights. I will keep in mind the naming
>> rules in Python, so as not to have it in collision with Python's
>> builtins.
>>
>> The mention to tidy up e.g. "compartmentalize" code is also noted. I
>> agree definitely.
>>
>> I'm reworking my code and will post back.
>>
>>     
>
> For reason unknown to me the script dont repeat itself
> when asked if to play again or not no matter what juggling of the
> while/if conditionals i do, the result is the same. What am i
> missing?
>
> Also, I'm not sure I'm right in the assignment ans = choose_Cave.
> I code it like so because the assignment ans = choose_Cave()
> the prompt 'Choose a cave' is repeated when the script is run.
> Dont want this. I tested this when run in ipython.
>
> I dont know if there is a way to just retrieve the return value to a
> variable AND not run the whole function i mean in the case of
> ans = choose_Cave(). Would love to hear your expert insights by return.
>
> http://paste.debian.net/51004/
>
>
>   
You have the following line at top-level:

    if ask.lower not in reply:

But you're not calling the method str.lower(), you're just creating a 
function object from it.  You need those parentheses.

    if ask.lower() not in reply:


You have the following fragment in main():

    print(intro)
    choose_Cave()
    ans = choose_Cave    # use: pass function by reference
    checkCave(ans)


which has two references to choose_Cave().  The first one calls it, but 
throws away the return value.  The second does not call it at all, but 
merely creates a function object and stores it in ans.  You need to 
combine your two intents in one line:

    print(intro)
    ans = choose_Cave()    # use: pass function by reference
    checkCave(ans)

Hope that helps.  Remember, that unlike Pascal, in Python you don't call 
the function unless you include the parentheses.

DaveA





More information about the Tutor mailing list