time module.
time.sleep() function.
return keyword.
def keyword.
and and or and not We've already used two functions in our previous programs: input() and print(). In our previous programs, we have called these functions to execute the code that is inside these functions. In this chapter, we will write our own functions for our programs to call. A function is like a mini-program that is inside of our program. Many times in a program we want to run the exact same code multiple times. Instead of typing out this code several times, we can put that code inside a function and call the function several times. This has the added benefit that if we make a
The game we will create to introduce functions is called "Dragon
In this game, the player is in a land full of dragons. The dragons all live in
Open a new file editor window by clicking on the File menu, then click on New Window. In the blank window that appears type in the source code and save the source code as dragon.py. Then run the program by pressing F5.
You are in a land full of dragons. In front of you, you see two caves. In one cave, the dragon is friendly and will share his treasure with you. The other dragon is greedy and hungry, and will eat you on sight. Which cave will you go into? (1 or 2) 1 You approach the cave... It is dark and spooky... A large dragon jumps out in front of you! He opens his jaws and... Gobbles you down in one bite! Do you want to play again? (yes or no) no
Here is the source code for the Dragon
One thing to know as you read through the code below: The blocks that follow the def lines define a function, but the code in that block does not run until the function is called. The code does not execute each line in this program in top down order. This will be explained in more detail later in this chapter.
dragon.py
This code can be downloaded from http://inventwithpyt.hon.com/dragon.py
If you get errors after typing this code in, compare it to the book's code with the online
diff tool at http://inventwithpython.com/diff or email the author at
al@inventwithpython.com
1. import random
2. import time 3 .
4. def displayIntro () :
5. print('You are on a planet full of dragons. In front of you,')
6. print('you see two caves. In one cave, the dragon is friendly')
7. print('and will share his treasure with you. The other dragon')
8. print('is greedy and hungry, and will eat you on sight.')
9. print() 10 .
11. def chooseCave() : 12 . cave = ' '
13. while cave != '1' and cave != '2':
14. print('Which cave will you go into? (1 or 2)')
15. cave = input () 16 .
17. return cave
18 .
19. def checkCave(chosenCave):
20. print('You approach the cave...')
21. time.sleep(2)
22. print('It is dark and spooky...')
23. time.sleep(2)
24. print('A large dragon jumps out in front of you! He opens his jaws and...')
25. print()
26. time.sleep(2)
27.
28. friendlyCave = random.randint(1, 2)
29.
30. if chosenCave == str(friendlyCave):
31. print('Gives you his treasure!')
32. else:
33. print('Gobbles you down in one bite!') 34 .
35. playAgain = 'yes'
36. while playAgain == 'yes' or playAgain == 'y': 37 .
38. displayIntro()
39.
40. caveNumber = chooseCave()
41.
42. checkCave(caveNumber) 43 .
44. print('Do you want to play again? (yes or no)')
45. playAgain = input()
Let's look at the source code in more detail.
1. import random 2. import time
Here we have two import statements. We import the random module like we did in the time module includes, so we will import that as well.
4 . def displayIntro():
5 . print('You are on a planet full of dragons. In front of you,')
6 . print('you see two caves. In one cave, the dragon is friendly')
7 . print('and will share his treasure with you. The other dragon')
8 . print ( 'is greedy and hungry, and will eat you on sight.')
9 . print()
Figure 6.1 shows a new type of statement, the def statement. The def statement is made up of the def keyword, followed by a function name with : sign). There is a block after the statement called the def-block.
(рис 6.1) Parts of a def statement.
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 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
11. def chooseCave() :
Here we are defining another function called chooseCave.
The code in this function will prompt the user to select which
12 . cave = ' ' 13. while cave != '1' and cave != '2':
Inside the chooseCave() function, we create a new variable called 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 == or != are
True and False. Boolean statements are always either true or false. If the statement is not true, then it is false. And if the statement is not false, then it is true.
* operator will combine two integer values and produce a new integer value (the product of the two original integers)? And do you also remember how the + operator can combine two strings and produce a new string value (the concatenation of the two original strings)? The and and operator works.
Think of the
But the
The and operator in Python works this way too. If the boolean values on both sides of the and keyword are True, then the expression with the and operator evaluates to True. If either of the boolean values are False, or both of the boolean values are False, then the expression evaluates to False.
So let's look at line 13 again:
13. while cave != '1' and cave != '2':
This condition is made up of two expressions connected by the and True or False) values. Then we evaluate the boolean values with the and operator.
The string value stored in ''. The blank string does not equal the string '1', so the left side evaluates to True.
The blank string also does not equal the string '2', so the right side evaluates to True. So the condition then turns into True and True. Because both boolean values are True, the condition finally evaluates to True. And because the while statement's condition is True, the program execution enters the while-block.
This is all done by the Python interpreter, but it is important to understand how the interpreter does this. This picture shows the steps of how the interpreter evaluates the condition (if the value of
while cave != '1' and cave != '2' :
while '' != '1' and cave != '2' :
while True and '' != '2' :
while True and True :
while True :
Try typing the following into the interactive shell:
>>> True and True True >>> True and False False >>> False and True False >>> False and False False
There are two other or operator. The or operator works similar to the and, except it will evaluate to True if either of the two boolean values are True. The only time the or operator evaluates to False is if both of the boolean values are False.
The
Try typing the following into the interactive shell:
>>> True or True True >>> True or False True >>> False or True True >>> False or False False
The third not. The not operator is different from every other operator we've seen before, because it only works on one value, not two. There is only value on the right side of the not keyword, and none on the left. The not operator will evaluate to True as False and will evaluate False as True.
Try typing the following into the interactive shell:
>>> not True False >>> not False True >>> True not SyntaxError: invalid syntax (<pyshell#0>, line 1)
Notice that if we put the boolean value on the left side of the not operator results in a
We can use both the and and not operators in a single expression. Try typing True and not False into the shell:
>>> True and not False True
Normally the expression True and False would evaluate to False. But the True and not False expression evaluates to True. This is because not False evaluates to True, which turns the expression into True and True, which evaluates to True.
If you ever forget how the
| A | and | B | is | Entire statement |
|---|---|---|---|---|
| True | and | True | is | True |
| True | and | False | is | False |
| False | and | True | is | False |
| False | and | False | is | False |
| A | or | B | is | Entire statement |
|---|---|---|---|---|
| True | or | True | is | True |
| True | or | False | is | True |
| False | or | True | is | True |
| False | or | False | is | False |
| not A | is | Entire statement |
|---|---|---|
| not True | is | False |
| not False | is | True |
14 . print('Which cave will cave = input ()
15 . you go into? (1 or 2)')
Here, the player is asked to enter which . After this code is executed, we jump back to the top of the while statement and recheck the condition. Remember that the line was:
13 . while cave != '1' and cave != '2':
If this condition evaluates to True, we will enter the while-block again and ask the player for a value will either be '1' or '2'. This causes the condition to evaluate to False, and the program execution will continue on past the while loop.
The reason we have a loop here is because the player may have typed in 3 or 4 or HELLO. Our program doesn't make sense of this, so if the player did not enter 1 or 2, then the program loops back and asks the player again. In fact, the computer will patiently ask the player for the False, and we will jump down past the while-block and continue with the program.
17. return cave
This is the return keyword, which only appears inside def-blocks. Remember how the input() function returns the string value that the player typed in? Or how the randint () function will return a random integer value? Our function will also return a value. It returns the string that is stored in .
This means that if we had a line of code like spam = chooseCave(), the code inside chooseCave() would be executed and the function call will evaluate to chooseCave()'s return value. The return value will either be the string '1' or the string '2'. (Our while loop chooseCave() will only return either '1' or '2'.)
The return keyword is only found inside def-blocks. Once the return statement is executed, we immediately jump out of the def-block. (This is like how the break statement will make us jump out of a while-block.) The program execution moves back to the line that had called the function.
You can also use the return keyword by itself just to break out of the function, just like the break keyword will break out of a while loop.
Just like the values in our program's variables are forgotten after the program ends, variables created inside the function are forgotten after the execution leaves the function. Not only that, but when execution is inside the function, we cannot change the variables outside of the function, or variables inside other functions. The variable's scope is this range that variables can be modified in. The only variables that we can use inside a function are the ones we create inside of the function (or the parameter variables, described later). That is, the scope of the variable is inside in the function's block. The scope of variables created outside of functions is outside of all functions in the program.
Not only that, but if we have a variable named spam created outside of a function, and we create a variable named spam inside of the function, the Python interpreter will consider them to be two separate variables. That means we can change the value of spam inside the function, and this will not change the spam variable that is outside of the function. This is because these variables have different scopes, the
We have names for these scopes. The scope outside of all functions is called the global scope. The scope inside of a function is called the local scope. The entire program has only one
Variables defined in the
Specifically, we can read the value of
Look at this example to see what happens when you try to change a funky() function isn't run until the funky() function is called. The comments explain what is going on:
# This block doesn't run until funky() is called:
def funky():
# We read the global variable's value:
print(spam) # 42
# We create a local variable named "spam"
# instead of changing the value of the global
# variable "spam":
spam = 99
# The name "spam" now refers to the local
# variable only for the rest of this
# function:
print(spam) # 99
# A global variable named "spam":
spam = 42
# Call the funky() function:
funky()
# The global variable was not changed in funky():
print(spam) # 42
It is important to know when a variable is defined because that is how we know the variable's scope. A variable is defined the first time we use it in an
12 . cave = ' '
...the variable is defined.
If we call the chooseCave() function twice, the value stored in the variable the first time won't be remember the second time around. This is because when the execution left the chooseCave() function (that is, left chooseCave()'s variable was forgotten and
The important thing to remember is that the value of a variable in the
19. def checkCave(chosenCave):
Now we are defining yet another function named checkCave(). Notice that we put the text chosenCave in between the
Remember, for some functions like for the str() or randint(), we would pass an argument in between the
>>> str(5) '5' >>> random.randint(1, 20) 14
When we call checkCave(), we will also pass one value to it as an argument. When execution moves inside the checkCave() function, a new variable named chosenCave will be assigned this value. This is how we pass variable values to functions since functions cannot read variables outside of the function (that is, outside of the function's
For example, here is a short program that
def sayHello(name):
print('Hello, ' + name)
print('Say hello to Alice.')
fizzy = 'Alice'
sayHello(fizzy)
print('Do not forget to say hello to Bob.')
sayHello('Bob')
If we run this program, it would look like this:
Say hello to Alice. Hello, Alice Do not forget to say hello to Bob. Hello, Bob
This program calls a function we have created, csayHello() and first passes the value in the fizzy variable as an argument to it. (We stored the string 'Alice' in fizzy.) Later, the program calls the sayHello() function again, passing the string 'Bob' as an argument.
The value in the fizzy variable and the string 'Bob' are arguments. The name variable in sayHello() is a parameter. The difference between arguments and parameters is that arguments are the values passed in a function call, and parameters are the
We could have just used the fizzy variable inside the sayHello() function instead of using a parameter. (This is because the fizzy variable a string each time before we call the sayHello() function. Parameters make our programs simpler. Look at this code:
def sayHello():
print('Hello, ' + fizzy)
print('Say hello to Alice.')
fizzy = 'Alice'
sayHello()
print('Do not forget to say hello to Bob.')
sayHello()
When we run this code, it looks like this:
Say hello to Alice. Hello, Alice Do not forget to say hello to Bob. Hello, Alice
This program's sayHello() function does not have a parameter, but uses the fizzy directly. Remember that you can read
Without parameters, we have to remember to set the fizzy variable before calling sayHello(). In this program, we forgot to do so, so the second time we called sayHello() the value of fizzy was still 'Alice'. Using parameters makes function calling simpler to do, especially when our programs are very big and have many functions.
Now look at the following program, which is a bit different. To make it clear to see, the
def spam(ImyName!):
print('Hello, ' + ImyName!)
ImyName! = 'Waffles'
print('Your new name is ' + ImyName!)
myName = 'Albert'
spam(myName)
print('Howdy, ' + myName)
If we run this program, it would look like this:
Hello, Albert Your new name is Waffles Howdy, Albert
This program defines a new variable called myName and stores the string 'Albert' in it. Then the program calls the spam() function, passing the value in myName as an argument. The execution moves to the spam() function. The parameter in spam() is also named myName, and has the argument value assigned to it. Remember, the myName inside the spam() function (the myName variable outside the function (the
The function then prints 'Hello, Albert', and then on the next line changes the value in myName to 'Waffles'. Remember, this only changes the local myName variable that is inside the function. The global myName variable that is outside the function still has the value 'Albert' stored in it.
The function now prints out 'Your new name is Waffles', because the myName variable in the 'Waffles'. The execution has reached the end of the function, so it jumps back down to where the function call was. The local myName is print('Howdy, ' + myName), which will display Howdy, Albert.
Remember, the myName outside of functions (that is, in the 'Albert', not 'Waffles'. This is because the myName in the myName in spam()'s
A function's definition (where we put the def statement and the def-block) has to come before you call the function. This is like how you must assign a value to a variable before you can use the variable. If you put the function call before the function definition, you will get an error. Look at this code:
sayGoodBye()
def sayGoodBye():
print('Good bye!')
If you try to run it, Python will give you an error message that looks like this:
Traceback (most recent call last): File "C:\Python31\foo.py", line 1, in <module> sayGoodBye() NameError: name 'sayGoodBye' is not defined
To fix this, put the function definition before the function call:
def sayGoodBye():
print('Good bye!')
sayGoodBye()
Back to the game's source code:
20. print('You approach the cave...')
21. time.sleep(2)
We display some text to the player, and then call the time.sleep() function. Remember how in our call to randint(), the function randint() is inside the random module? In the Dragon time module. The time module has a function called sleep() that will pause the program for a few seconds. We pass the integer value 2 as an argument to the time.sleep() function to tell it to pause for exactly 2 seconds.
22 . print('It is dark and spooky...')
23. time.sleep(2)
Here we print some more text and wait again for another 2 seconds. These short pauses add input() function to wait until the player pressed the Enter key. Here, the player doesn't have to do anything at all except wait.
24. print('A large dragon jumps out in front of you! He
opens his jaws and...')
25 . print()
26. time.sleep(2)
What happens next? And how does the program decide what happens?
28. friendlyCave = random.randint(1, 2)
Now we are going to have the program randomly chose which random.randint() function will return either the integer 1 or the integer 2, and store this value in a variable called friendlyCave.
30. if chosenCave == str(friendlyCave):
31. print('Gives you his treasure!')
Here we check if the integer of the ('1' or '2') is equal to the chosenCave was a string (because input() returns strings) and the value in friendlyCave is an integer (because random.randint() returns integers). We can't compare strings and integers with the == sign, because they will always be different ('1' does not equal 1).
Comparing values of different data types with the == operator will always evaluate to False.
So we are passing friendlyCave to the str() function, which returns the string value of friendlyCave.
What the condition in this if statement is really comparing is the string in chosenCave and the string returned by the str() function. We could have also had this line instead:
if int(chosenCave) == friendlyCave:
Then the if statement's condition would compare the integer value returned by the int () function to the integer value in friendlyCave. The return value of the int() function is the integer form of the string stored in chosenCave.
If the if statement's condition evaluates to True, we tell the player they have won the treasure.
32. else:
33. print('Gobbles you down in one bite!')
Line 32 has a new keyword. The else keyword always comes after the if-block. The else-block that follows the else keyword executes if the condition in the if statement was False. Think of it as the program's way of saying, "If this condition is true then execute the if-block or else execute the else-block."
Remember to put the colon (the : sign) after the else keyword.
You may have noticed that we always place a colon at the end of if, else, while, and def statements. The colon marks the end of the statement, and tells us that the next line should be the beginning of a new block.
35. playAgain = 'yes'
This is the first line that is not a def statement or inside a def-block. This line is where our program really begins. The previous def statements merely defined the functions, it did not run the code inside of the functions. Programs must always define functions before the function can be called. This is exactly like how variables must be defined with an
36. while playAgain == 'yes' or playAgain == 'y':
Here is the beginning of a while loop. We enter the loop if playAgain is equal to either 'yes' or 'y'. The first time we come to this while statement, we have just assigned the string value 'yes' to the playAgain variable. That means this condition will be True.
38. displayIntro()
Here we call the displayIntro() function. This isn't a Python function, it is our function that we defined earlier in our program. When this function is called, the program execution jumps to the first line in the displayIntro() function on line 5. When all the lines in the function are done, the execution jumps back down to the line after this one.
40. caveNumber = chooseCave()
This line also calls a function that we created. Remember that the chooseCave() function lets the player type in the 's value is the return value of this function. The return value is stored in a new variable named caveNumber. Then the execution moves to the next line.
42. checkCave(caveNumber)
This line calls our checkCave() function with the argument of caveNumber's value. Not only does execution jump to line 20, but the value stored in caveNumber is copied to the parameter chosenCave inside the checkCave() function. This is the function that will display either 'Gives you his treasure!' or 'Gobbles you down in one bite!', depending on the
44. print('Do you want to play again? (yes or no)')
45. playAgain = input()
After the game has been played, the player is asked if they would like to play again. The variable playAgain stores the string that the user typed in.
Then we reach the end of the while-block, so the program rechecks the while statement's condition: while playAgain == 'yes' or playAgain == 'y'
The difference is, now the value of playAgain is equal to whatever string the player typed in. If the player typed in the string 'yes' or 'y', then we would enter the loop again at line 38.
If the player typed in 'no' or 'n' or something silly like 'Abraham Lincoln', then the while statement's condition would be False, and we would go to the next line after the while-block. But since there are no more lines after the while-block, the program terminates.
But remember, the string 'YES' is different from the string 'yes'. If the player typed in the string 'YES', then the while statement's condition would evaluate to False and the program would still terminate.
We've just completed our second game! In our Dragon
We went through the source code from top to bottom. If you would like to go through the source code in the order that the execution flows, then check out the online tracing web site for this program at the URL http://inventwithpython.com/traces/dragon.html.
Dragon
For example, it may help to draw a flow chart. A flow chart is a picture that shows every possible action that can happen in our game, and in what order. Normally we would create a flow chart before writing our program, so that we remember to write code for each thing that happens in the game. Figure 6.2 is a flow chart for Dragon
(рис 6.2) Flow chart for the Dragon Realm game.
To see what happens in the game, put your finger on the "Start" box and follow one arrow from the box to another box. Your finger is kind of like the program execution. Your finger will trace out a path from box to box, until finally your finger lands on the "End" box. As you can see, when you get to the "Check for friendly or hungry dragon" box, the program could either go to the "Player wins" box or the "Player loses" box. Either way, both paths will end up at the "Ask to play again" box, and from there the program will either end or show the introduction to the player again.
In the "Dragon
The inputs for functions are the arguments we pass when we make a function call. The function call itself evaluates to a value called the return value. The return value is the output of the function.
We also learned about variable scopes. Variables that are created inside of a function exist in the
Variable scopes might seem complicated, but they are very useful for organizing functions as pieces of code that are separate from the rest of the function. Because each function has it's own
All nontrivial programs use functions because they are so useful, including the rest of the games in this book. By understanding how functions work, we can save ourselves a lot of typing and make our programs easier to read later on.
time module.
time.sleep() function.
return keyword.
def keyword.
and and or and not We've already used two functions in our previous programs: input() and print(). In our previous programs, we have called these functions to execute the code that is inside these functions. In this chapter, we will write our own functions for our programs to call. A function is like a mini-program that is inside of our program. Many times in a program we want to run the exact same code multiple times. Instead of typing out this code several times, we can put that code inside a function and call the function several times. This has the added benefit that if we make a
The game we will create to introduce functions is called "Dragon
In this game, the player is in a land full of dragons. The dragons all live in
Open a new file editor window by clicking on the File menu, then click on New Window. In the blank window that appears type in the source code and save the source code as dragon.py. Then run the program by pressing F5.
You are in a land full of dragons. In front of you, you see two caves. In one cave, the dragon is friendly and will share his treasure with you. The other dragon is greedy and hungry, and will eat you on sight. Which cave will you go into? (1 or 2) 1 You approach the cave... It is dark and spooky... A large dragon jumps out in front of you! He opens his jaws and... Gobbles you down in one bite! Do you want to play again? (yes or no) no
Here is the source code for the Dragon
One thing to know as you read through the code below: The blocks that follow the def lines define a function, but the code in that block does not run until the function is called. The code does not execute each line in this program in top down order. This will be explained in more detail later in this chapter.
dragon.py
This code can be downloaded from http://inventwithpyt.hon.com/dragon.py
If you get errors after typing this code in, compare it to the book's code with the online
diff tool at http://inventwithpython.com/diff or email the author at
al@inventwithpython.com
1. import random
2. import time 3 .
4. def displayIntro () :
5. print('You are on a planet full of dragons. In front of you,')
6. print('you see two caves. In one cave, the dragon is friendly')
7. print('and will share his treasure with you. The other dragon')
8. print('is greedy and hungry, and will eat you on sight.')
9. print() 10 .
11. def chooseCave() : 12 . cave = ' '
13. while cave != '1' and cave != '2':
14. print('Which cave will you go into? (1 or 2)')
15. cave = input () 16 .
17. return cave
18 .
19. def checkCave(chosenCave):
20. print('You approach the cave...')
21. time.sleep(2)
22. print('It is dark and spooky...')
23. time.sleep(2)
24. print('A large dragon jumps out in front of you! He opens his jaws and...')
25. print()
26. time.sleep(2)
27.
28. friendlyCave = random.randint(1, 2)
29.
30. if chosenCave == str(friendlyCave):
31. print('Gives you his treasure!')
32. else:
33. print('Gobbles you down in one bite!') 34 .
35. playAgain = 'yes'
36. while playAgain == 'yes' or playAgain == 'y': 37 .
38. displayIntro()
39.
40. caveNumber = chooseCave()
41.
42. checkCave(caveNumber) 43 .
44. print('Do you want to play again? (yes or no)')
45. playAgain = input()
Let's look at the source code in more detail.
1. import random 2. import time
Here we have two import statements. We import the random module like we did in the time module includes, so we will import that as well.
4 . def displayIntro():
5 . print('You are on a planet full of dragons. In front of you,')
6 . print('you see two caves. In one cave, the dragon is friendly')
7 . print('and will share his treasure with you. The other dragon')
8 . print ( 'is greedy and hungry, and will eat you on sight.')
9 . print()
Figure 6.1 shows a new type of statement, the def statement. The def statement is made up of the def keyword, followed by a function name with : sign). There is a block after the statement called the def-block.
(рис 6.1) Parts of a def statement.
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 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
11. def chooseCave() :
Here we are defining another function called chooseCave.
The code in this function will prompt the user to select which
12 . cave = ' ' 13. while cave != '1' and cave != '2':
Inside the chooseCave() function, we create a new variable called 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 == or != are
True and False. Boolean statements are always either true or false. If the statement is not true, then it is false. And if the statement is not false, then it is true.
* operator will combine two integer values and produce a new integer value (the product of the two original integers)? And do you also remember how the + operator can combine two strings and produce a new string value (the concatenation of the two original strings)? The and and operator works.
Think of the
But the
The and operator in Python works this way too. If the boolean values on both sides of the and keyword are True, then the expression with the and operator evaluates to True. If either of the boolean values are False, or both of the boolean values are False, then the expression evaluates to False.
So let's look at line 13 again:
13. while cave != '1' and cave != '2':
This condition is made up of two expressions connected by the and True or False) values. Then we evaluate the boolean values with the and operator.
The string value stored in ''. The blank string does not equal the string '1', so the left side evaluates to True.
The blank string also does not equal the string '2', so the right side evaluates to True. So the condition then turns into True and True. Because both boolean values are True, the condition finally evaluates to True. And because the while statement's condition is True, the program execution enters the while-block.
This is all done by the Python interpreter, but it is important to understand how the interpreter does this. This picture shows the steps of how the interpreter evaluates the condition (if the value of
while cave != '1' and cave != '2' :
while '' != '1' and cave != '2' :
while True and '' != '2' :
while True and True :
while True :
Try typing the following into the interactive shell:
>>> True and True True >>> True and False False >>> False and True False >>> False and False False
There are two other or operator. The or operator works similar to the and, except it will evaluate to True if either of the two boolean values are True. The only time the or operator evaluates to False is if both of the boolean values are False.
The
Try typing the following into the interactive shell:
>>> True or True True >>> True or False True >>> False or True True >>> False or False False
The third not. The not operator is different from every other operator we've seen before, because it only works on one value, not two. There is only value on the right side of the not keyword, and none on the left. The not operator will evaluate to True as False and will evaluate False as True.
Try typing the following into the interactive shell:
>>> not True False >>> not False True >>> True not SyntaxError: invalid syntax (<pyshell#0>, line 1)
Notice that if we put the boolean value on the left side of the not operator results in a
We can use both the and and not operators in a single expression. Try typing True and not False into the shell:
>>> True and not False True
Normally the expression True and False would evaluate to False. But the True and not False expression evaluates to True. This is because not False evaluates to True, which turns the expression into True and True, which evaluates to True.
If you ever forget how the
| A | and | B | is | Entire statement |
|---|---|---|---|---|
| True | and | True | is | True |
| True | and | False | is | False |
| False | and | True | is | False |
| False | and | False | is | False |
| A | or | B | is | Entire statement |
|---|---|---|---|---|
| True | or | True | is | True |
| True | or | False | is | True |
| False | or | True | is | True |
| False | or | False | is | False |
| not A | is | Entire statement |
|---|---|---|
| not True | is | False |
| not False | is | True |
14 . print('Which cave will cave = input ()
15 . you go into? (1 or 2)')
Here, the player is asked to enter which . After this code is executed, we jump back to the top of the while statement and recheck the condition. Remember that the line was:
13 . while cave != '1' and cave != '2':
If this condition evaluates to True, we will enter the while-block again and ask the player for a value will either be '1' or '2'. This causes the condition to evaluate to False, and the program execution will continue on past the while loop.
The reason we have a loop here is because the player may have typed in 3 or 4 or HELLO. Our program doesn't make sense of this, so if the player did not enter 1 or 2, then the program loops back and asks the player again. In fact, the computer will patiently ask the player for the False, and we will jump down past the while-block and continue with the program.
17. return cave
This is the return keyword, which only appears inside def-blocks. Remember how the input() function returns the string value that the player typed in? Or how the randint () function will return a random integer value? Our function will also return a value. It returns the string that is stored in .
This means that if we had a line of code like spam = chooseCave(), the code inside chooseCave() would be executed and the function call will evaluate to chooseCave()'s return value. The return value will either be the string '1' or the string '2'. (Our while loop chooseCave() will only return either '1' or '2'.)
The return keyword is only found inside def-blocks. Once the return statement is executed, we immediately jump out of the def-block. (This is like how the break statement will make us jump out of a while-block.) The program execution moves back to the line that had called the function.
You can also use the return keyword by itself just to break out of the function, just like the break keyword will break out of a while loop.
Just like the values in our program's variables are forgotten after the program ends, variables created inside the function are forgotten after the execution leaves the function. Not only that, but when execution is inside the function, we cannot change the variables outside of the function, or variables inside other functions. The variable's scope is this range that variables can be modified in. The only variables that we can use inside a function are the ones we create inside of the function (or the parameter variables, described later). That is, the scope of the variable is inside in the function's block. The scope of variables created outside of functions is outside of all functions in the program.
Not only that, but if we have a variable named spam created outside of a function, and we create a variable named spam inside of the function, the Python interpreter will consider them to be two separate variables. That means we can change the value of spam inside the function, and this will not change the spam variable that is outside of the function. This is because these variables have different scopes, the
We have names for these scopes. The scope outside of all functions is called the global scope. The scope inside of a function is called the local scope. The entire program has only one
Variables defined in the
Specifically, we can read the value of
Look at this example to see what happens when you try to change a funky() function isn't run until the funky() function is called. The comments explain what is going on:
# This block doesn't run until funky() is called:
def funky():
# We read the global variable's value:
print(spam) # 42
# We create a local variable named "spam"
# instead of changing the value of the global
# variable "spam":
spam = 99
# The name "spam" now refers to the local
# variable only for the rest of this
# function:
print(spam) # 99
# A global variable named "spam":
spam = 42
# Call the funky() function:
funky()
# The global variable was not changed in funky():
print(spam) # 42
It is important to know when a variable is defined because that is how we know the variable's scope. A variable is defined the first time we use it in an
12 . cave = ' '
...the variable is defined.
If we call the chooseCave() function twice, the value stored in the variable the first time won't be remember the second time around. This is because when the execution left the chooseCave() function (that is, left chooseCave()'s variable was forgotten and
The important thing to remember is that the value of a variable in the
19. def checkCave(chosenCave):
Now we are defining yet another function named checkCave(). Notice that we put the text chosenCave in between the
Remember, for some functions like for the str() or randint(), we would pass an argument in between the
>>> str(5) '5' >>> random.randint(1, 20) 14
When we call checkCave(), we will also pass one value to it as an argument. When execution moves inside the checkCave() function, a new variable named chosenCave will be assigned this value. This is how we pass variable values to functions since functions cannot read variables outside of the function (that is, outside of the function's
For example, here is a short program that
def sayHello(name):
print('Hello, ' + name)
print('Say hello to Alice.')
fizzy = 'Alice'
sayHello(fizzy)
print('Do not forget to say hello to Bob.')
sayHello('Bob')
If we run this program, it would look like this:
Say hello to Alice. Hello, Alice Do not forget to say hello to Bob. Hello, Bob
This program calls a function we have created, csayHello() and first passes the value in the fizzy variable as an argument to it. (We stored the string 'Alice' in fizzy.) Later, the program calls the sayHello() function again, passing the string 'Bob' as an argument.
The value in the fizzy variable and the string 'Bob' are arguments. The name variable in sayHello() is a parameter. The difference between arguments and parameters is that arguments are the values passed in a function call, and parameters are the
We could have just used the fizzy variable inside the sayHello() function instead of using a parameter. (This is because the fizzy variable a string each time before we call the sayHello() function. Parameters make our programs simpler. Look at this code:
def sayHello():
print('Hello, ' + fizzy)
print('Say hello to Alice.')
fizzy = 'Alice'
sayHello()
print('Do not forget to say hello to Bob.')
sayHello()
When we run this code, it looks like this:
Say hello to Alice. Hello, Alice Do not forget to say hello to Bob. Hello, Alice
This program's sayHello() function does not have a parameter, but uses the fizzy directly. Remember that you can read
Without parameters, we have to remember to set the fizzy variable before calling sayHello(). In this program, we forgot to do so, so the second time we called sayHello() the value of fizzy was still 'Alice'. Using parameters makes function calling simpler to do, especially when our programs are very big and have many functions.
Now look at the following program, which is a bit different. To make it clear to see, the
def spam(ImyName!):
print('Hello, ' + ImyName!)
ImyName! = 'Waffles'
print('Your new name is ' + ImyName!)
myName = 'Albert'
spam(myName)
print('Howdy, ' + myName)
If we run this program, it would look like this:
Hello, Albert Your new name is Waffles Howdy, Albert
This program defines a new variable called myName and stores the string 'Albert' in it. Then the program calls the spam() function, passing the value in myName as an argument. The execution moves to the spam() function. The parameter in spam() is also named myName, and has the argument value assigned to it. Remember, the myName inside the spam() function (the myName variable outside the function (the
The function then prints 'Hello, Albert', and then on the next line changes the value in myName to 'Waffles'. Remember, this only changes the local myName variable that is inside the function. The global myName variable that is outside the function still has the value 'Albert' stored in it.
The function now prints out 'Your new name is Waffles', because the myName variable in the 'Waffles'. The execution has reached the end of the function, so it jumps back down to where the function call was. The local myName is print('Howdy, ' + myName), which will display Howdy, Albert.
Remember, the myName outside of functions (that is, in the 'Albert', not 'Waffles'. This is because the myName in the myName in spam()'s
A function's definition (where we put the def statement and the def-block) has to come before you call the function. This is like how you must assign a value to a variable before you can use the variable. If you put the function call before the function definition, you will get an error. Look at this code:
sayGoodBye()
def sayGoodBye():
print('Good bye!')
If you try to run it, Python will give you an error message that looks like this:
Traceback (most recent call last): File "C:\Python31\foo.py", line 1, in <module> sayGoodBye() NameError: name 'sayGoodBye' is not defined
To fix this, put the function definition before the function call:
def sayGoodBye():
print('Good bye!')
sayGoodBye()
Back to the game's source code:
20. print('You approach the cave...')
21. time.sleep(2)
We display some text to the player, and then call the time.sleep() function. Remember how in our call to randint(), the function randint() is inside the random module? In the Dragon time module. The time module has a function called sleep() that will pause the program for a few seconds. We pass the integer value 2 as an argument to the time.sleep() function to tell it to pause for exactly 2 seconds.
22 . print('It is dark and spooky...')
23. time.sleep(2)
Here we print some more text and wait again for another 2 seconds. These short pauses add input() function to wait until the player pressed the Enter key. Here, the player doesn't have to do anything at all except wait.
24. print('A large dragon jumps out in front of you! He
opens his jaws and...')
25 . print()
26. time.sleep(2)
What happens next? And how does the program decide what happens?
28. friendlyCave = random.randint(1, 2)
Now we are going to have the program randomly chose which random.randint() function will return either the integer 1 or the integer 2, and store this value in a variable called friendlyCave.
30. if chosenCave == str(friendlyCave):
31. print('Gives you his treasure!')
Here we check if the integer of the ('1' or '2') is equal to the chosenCave was a string (because input() returns strings) and the value in friendlyCave is an integer (because random.randint() returns integers). We can't compare strings and integers with the == sign, because they will always be different ('1' does not equal 1).
Comparing values of different data types with the == operator will always evaluate to False.
So we are passing friendlyCave to the str() function, which returns the string value of friendlyCave.
What the condition in this if statement is really comparing is the string in chosenCave and the string returned by the str() function. We could have also had this line instead:
if int(chosenCave) == friendlyCave:
Then the if statement's condition would compare the integer value returned by the int () function to the integer value in friendlyCave. The return value of the int() function is the integer form of the string stored in chosenCave.
If the if statement's condition evaluates to True, we tell the player they have won the treasure.
32. else:
33. print('Gobbles you down in one bite!')
Line 32 has a new keyword. The else keyword always comes after the if-block. The else-block that follows the else keyword executes if the condition in the if statement was False. Think of it as the program's way of saying, "If this condition is true then execute the if-block or else execute the else-block."
Remember to put the colon (the : sign) after the else keyword.
You may have noticed that we always place a colon at the end of if, else, while, and def statements. The colon marks the end of the statement, and tells us that the next line should be the beginning of a new block.
35. playAgain = 'yes'
This is the first line that is not a def statement or inside a def-block. This line is where our program really begins. The previous def statements merely defined the functions, it did not run the code inside of the functions. Programs must always define functions before the function can be called. This is exactly like how variables must be defined with an
36. while playAgain == 'yes' or playAgain == 'y':
Here is the beginning of a while loop. We enter the loop if playAgain is equal to either 'yes' or 'y'. The first time we come to this while statement, we have just assigned the string value 'yes' to the playAgain variable. That means this condition will be True.
38. displayIntro()
Here we call the displayIntro() function. This isn't a Python function, it is our function that we defined earlier in our program. When this function is called, the program execution jumps to the first line in the displayIntro() function on line 5. When all the lines in the function are done, the execution jumps back down to the line after this one.
40. caveNumber = chooseCave()
This line also calls a function that we created. Remember that the chooseCave() function lets the player type in the 's value is the return value of this function. The return value is stored in a new variable named caveNumber. Then the execution moves to the next line.
42. checkCave(caveNumber)
This line calls our checkCave() function with the argument of caveNumber's value. Not only does execution jump to line 20, but the value stored in caveNumber is copied to the parameter chosenCave inside the checkCave() function. This is the function that will display either 'Gives you his treasure!' or 'Gobbles you down in one bite!', depending on the
44. print('Do you want to play again? (yes or no)')
45. playAgain = input()
After the game has been played, the player is asked if they would like to play again. The variable playAgain stores the string that the user typed in.
Then we reach the end of the while-block, so the program rechecks the while statement's condition: while playAgain == 'yes' or playAgain == 'y'
The difference is, now the value of playAgain is equal to whatever string the player typed in. If the player typed in the string 'yes' or 'y', then we would enter the loop again at line 38.
If the player typed in 'no' or 'n' or something silly like 'Abraham Lincoln', then the while statement's condition would be False, and we would go to the next line after the while-block. But since there are no more lines after the while-block, the program terminates.
But remember, the string 'YES' is different from the string 'yes'. If the player typed in the string 'YES', then the while statement's condition would evaluate to False and the program would still terminate.
We've just completed our second game! In our Dragon
We went through the source code from top to bottom. If you would like to go through the source code in the order that the execution flows, then check out the online tracing web site for this program at the URL http://inventwithpython.com/traces/dragon.html.
Dragon
For example, it may help to draw a flow chart. A flow chart is a picture that shows every possible action that can happen in our game, and in what order. Normally we would create a flow chart before writing our program, so that we remember to write code for each thing that happens in the game. Figure 6.2 is a flow chart for Dragon
(рис 6.2) Flow chart for the Dragon Realm game.
To see what happens in the game, put your finger on the "Start" box and follow one arrow from the box to another box. Your finger is kind of like the program execution. Your finger will trace out a path from box to box, until finally your finger lands on the "End" box. As you can see, when you get to the "Check for friendly or hungry dragon" box, the program could either go to the "Player wins" box or the "Player loses" box. Either way, both paths will end up at the "Ask to play again" box, and from there the program will either end or show the introduction to the player again.
In the "Dragon
The inputs for functions are the arguments we pass when we make a function call. The function call itself evaluates to a value called the return value. The return value is the output of the function.
We also learned about variable scopes. Variables that are created inside of a function exist in the
Variable scopes might seem complicated, but they are very useful for organizing functions as pieces of code that are separate from the rest of the function. Because each function has it's own
All nontrivial programs use functions because they are so useful, including the rest of the games in this book. By understanding how functions work, we can save ourselves a lot of typing and make our programs easier to read later on.
Для получения официальных документов о завершении программы дополнительного профессионального образования (удостоверения о повышении квалификации, дипломов о профессиональной переподготовке и MBA) необходимо предоставить:
Внимание! Вы можете не заказывать доставку бумажной версии официального документы, а скачать его в электронном виде и распечатать самостоятельно. Информация о выданном документе в течение 1 месяца загружается в Федеральную информационную систему «Федеральный реестр сведений о документах об образовании и (или) о квалификации, документах об обучении» - ФИС ФРДО.
Доступ на новый сайт осуществляется с использованием адреса электронной почты, который был указан вами при регистрации на "старом". Мы постарались перенести все ваши данные с прежнего ресурса, однако не исключена вероятность потери части информации.
При возникновении проблемы со входом, воспользуйтесь функцией сброса пароля
Если вы обнаружите несоответствия, пожалуйста, сообщите нам.