[Tutor] Help
Chris Kavanagh
ckava1 at msn.com
Tue Nov 1 04:07:13 CET 2011
Yes Steven, that solved my question(s). It also cleared up what was to
be my next question! Thanks so much. You might not realize how grateful
I am to be able to have you & others on the list answer my questions.
Just trust me when I say, I am grateful. And I apologize for the code
being mangled on your end. I'm not sure why this happened, but you were
correct in your assumptions. . .
One thing I'm curious about. If the code is read by the Interpreter or
Compiler from the top down, why in this case, does it not get confused
(cause an error) when it hits line 30, if it doesn't yet know that
{caveNumber=chooseCave()} and checkCave(caveNumber)}?? It seems to me in
my limited experience, those last two lines should've come before line
30. It seems as though it would've made more sense to somehow put them
before. In other languages, ala C++, don't global variables have to be
declared at the 'top' of the code??
On 10/31/2011 9:55 PM, Steven D'Aprano wrote:
> Chris Kavanagh wrote:
>
>> However, I'm confused on Line 30 {if chosenCave== str(friendlyCave)}.
>> Here's the description of this line the author gives:
>>
>> "Here we check if the integer of the cave we chose ('1' or '2') is
>> equal to the cave
>> randomly selected to have the friendly dragon"
>>
>> My question is, we saved the integer of the cave we chose in the
>> variable {cave}in line 15, not {chosenCave}. So, what the heck am I
>> missing?? How is the {chosenCave} variable now holding the choice I
>> made in the {cave} variable??
>
> Unfortunately, the indentation of your code is completely mangled for
> me, which makes it difficult to be sure which parts of the code are
> inside functions and which are not. So I will be forced to guess.
>
> I can tell that line 15 is inside the function chooseCave(), and so the
> variable "cave" is a local variable. Local variables only exist inside
> the function that creates them. In this case, the chooseCave() function
> returns the value of "cave" to the caller.
>
> That is, at the end of your code, you call the functions you earlier
> created:
>
> caveNumber = chooseCave()
> checkCave(caveNumber)
>
>
> These two lines cause the following to happen:
>
> The function chooseCave() gets called. Execution shifts into the
> function chooseCave:
>
> 1 you are asked for a cave number
> 2 your response is stored temporarily in the local variable "cave"
> 3 and then returned to the caller
>
> At this point, Python clears up the local variables, reclaiming their
> memory ready for next time they are needed, and stores your response in
> the global (top level) variable "caveNumber".
>
> Next, you call the checkCave function with "caveNumber" as an argument.
> This gets passed to the checkCave function, which sees it under the
> local variable name "chosenCave". *Inside* the checkCave function, the
> value which is known *outside* as "caveNumber" is known as "chosenCave".
> And so inside the function, the line:
>
> if chosenCave== str(friendlyCave)
>
> works and the chosen cave number (known as "caveNumber" on the outside
> and "chosenCave" on the inside) is compared to the friendly cave.
>
> This might seem a bit confusing at first, but stick with it, it will
> soon become completely natural.
>
> Having different names for the same value in different contexts is a
> good thing. It's a bit like how the same person might be called:
>
> Mr President
> Barrack Obama
> Mr Obama
> Barrack
> Dad
> Son
> POTUS
> Bazza
>
> depending on who is referring to him and under what circumstances. (That
> last one might only be in Australia...)
>
> The important thing to remember is this:
>
>
> *Outside* of a function, all variables are *global*. You can't have two
> global variables called "x" at the same time:
>
> x = 1
> x = 2
>
> The second line replaces the value of x with a new value.
>
> *Inside* a function, all variable assignments are *local*. (You can
> change that with the "global" keyword, but you shouldn't.) A bit like
> going to Los Angeles, what happens inside a function stays inside the
> function. Local variable "x" doesn't interfere with global variable "x",
> or with local "x" of any other function. The only way to get the value
> of local "x" out and pass it to another function is with the return
> statement.
>
>
> I hope this helps.
>
>
>
More information about the Tutor
mailing list