Game Development Reference
In-Depth Information
def Statements
The def statement isn't a call to a function named displayIntro() . Instead, the def
statement means we are creating, or defining , a new function that we can call later in our
program. After we define this function, we can call it the same way we call other functions.
When we call this function, the code inside the def-block will be executed.
We also say we define variables when we create them with an assignment statement. The
code spam = 42 defines the variable spam .
Remember, the def statement doesn't execute the code right now, it only defines what
code is executed when we call the displayIntro() function later in the program.
When the program's execution reaches a def statement, it skips down to the end of the def-
block. We will jump back to the top of the def-block when the displayIntro()
function is called. It will then execute all the print() statements inside the def-block. So
we call this function when we want to display the "You are on a planet full of dragons..."
introduction to the user.
When we call the displayIntro() function, the program's execution jumps to the
start of the function on line 5. When the function's block ends, the program's execution
returns to the line that called the function.
We will explain all of the functions that this program will use before we explain the main
part of the program. It may be a bit confusing to learn the program out of the order that it
executes. But just keep in mind that when we define the functions they just silently sit
around waiting to be called into action.
Defining the chooseCave() Function
11. def chooseCave():
Here we are defining another function called chooseCave . The code in this function
will prompt the user to select which cave they should go into.
12. cave = ''
13. while cave != '1' and cave != '2':
Inside the chooseCave() function, we create a new variable called cave and store a
blank string in it. Then we will start a while loop. This while statement's condition
contains a new operator we haven't seen before called and . Just like the - or * are
mathematical operators, and == or != are comparison operators, the and operator is a
boolean operator.
Search WWH ::




Custom Search