[Tutor] Help

Steven D'Aprano steve at pearwood.info
Tue Nov 1 02:55:47 CET 2011

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 

caveNumber = chooseCave()

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

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 

I hope this helps.


More information about the Tutor mailing list