Commits

Allan Davis committed 3149dcf

generated 3 chapters from html

  • Participants
  • Parent commits c7991fd

Comments (0)

Files changed (5)

dragons_realm.rst

+`Go to Chapter 5 - Jokes`_`Go to Chapter 7 - Using the Debugger`_
+
+`.. image:: images/buyad.png
+`_
+
+.. image:: images/chap6.png
+
+
+
+Topics Covered In This Chapter:
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+-   The time module.
+-   The time.sleep() function.
+-   The return keyword.
+-   Creating our own functions with the def keyword.
+-   The and and or and not Boolean operators.
+-   Truth tables
+-   Variable scope (Global and Local)
+-   Parameters and Arguments
+-   Flow charts
+
+
+Introducing Functions
+---------------------
+
+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 mistake, we only have one
+place in the code to change it.
+
+The game we will create to introduce functions is called "Dragon Realm", and
+lets the player make a guess between two caves which randomly hold treasure
+or certain doom.
+
+
+How to Play "Dragon Realm"
+--------------------------
+
+In this game, the player is in a land full of dragons. The dragons all live
+in caves with their large piles of collected treasure. Some dragons are
+friendly, and will share their treasure with you. Other dragons are greedy
+and hungry, and will eat anyone who enters their cave. The player is in front
+of two caves, one with a friendly dragon and the other with a hungry dragon.
+The player is given a choice between the two.
+
+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.
+
+
+Sample Run of Dragon Realm
+--------------------------
+
+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
+
+
+
+Dragon Realm's Source Code
+--------------------------
+
+Here is the source code for the Dragon Realm game. Typing in the source code
+is a great way to get used to the code. But if you don't want to do all this
+typing, you can download the source code from this book's website at the URL
+`http://inventwithpython.com/chapter6`_. There are instructions on the
+website that will tell you how to download and open the source code file. If
+you type in the code yourself, you can use the online diff tool on the
+website to check for any mistakes in your code.
+
+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.
+
+**Important Note!** Be sure to run this program with Python 3, and not Python
+2. The programs in this book use Python 3, and you'll get errors if you try
+to run them with Python 2. You can click on Help and then About IDLE to find
+out what version of Python you have.
+
+dragon.py
+This code can be downloaded from `http://inventwithpython.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 in a land 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()
+
+
+How the Code Works
+------------------
+
+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 Guess the Number game. In Dragon Realm, we will also want some time-
+related functions that the time module includes, so we will import that as
+well.
+
+
+Defining the displayIntro() Function
+~~~~~~~~~
+
+1.  def displayIntro():
+2.      print('You are in a land full of dragons. In front of you,')
+3.      print('you see two caves. In one cave, the dragon is friendly')
+4.      print('and will share his treasure with you. The other dragon')
+5.      print('is greedy and hungry, and will eat you on sight.')
+6.      print()
+
+.. image:: images/6-1.png
+
+Figure 6-1: Parts of a def statement.
+
+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
+parentheses, and then a colon (the : sign). There is a block after the
+statement called the def-block.
+
+
+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 in a land 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
+~~~~~~~~~
+
+1.  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.
+
+1.      cave = ''
+2.      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.
+
+
+Boolean Operators
+-----------------
+
+Boolean logic deals with things that are either true or false. This is why
+the Boolean data type only has two values, True and False. Boolean
+expressions are always either True or False. If the expression is not True,
+then it is False. And if the expression is not False, then it is True.
+
+Boolean operators compare two Boolean values (also called bools) and evaluate
+to a single Boolean value. Do you remember how the * 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 Boolean operator combines two Boolean values to
+produce a new Boolean value. Here's how the and operator works.
+
+Think of the sentence, "Cats have whiskers and dogs have tails." This
+sentence is true, because "cats have whiskers" is true and "dogs have tails"
+is also true.
+
+But the sentence, "Cats have whiskers and dogs have wings" would be false.
+Even though "cats have whiskers" is true, dogs do not have wings, so "dogs
+have wings" is false. The entire sentence is only true if both parts are true
+because the two parts are connected by the word "and." If one or both parts
+are false, then the entire sentence is false.
+
+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.
+
+
+Evaluating an Expression That Contains Boolean Operators
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+So let's look at line 13 again:
+
+1.      while cave != '1' and cave != '2':
+
+This condition is has two expressions connected by the and Boolean operator.
+We first evaluate these expressions to get their Boolean (that is, True or
+False) values. Then we evaluate the Boolean values with the and operator.
+
+The string value stored in cave when we first execute this while statement is
+the blank string, ''. 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 cave is the blank
+string):
+
+while cave != '1' and cave != '2':
+    .. image:: images/downarrow.png
+    :alt: A downward arrow
+
+while '' != '1' and cave != '2':
+    .. image:: images/downarrow.png
+    :alt: A downward arrow
+
+while True and cave != '2':
+    .. image:: images/downarrow.png
+    :alt: A downward arrow
+
+while True and '' != '2':
+    .. image:: images/downarrow.png
+    :alt: A downward arrow
+
+while True and True:
+    .. image:: images/downarrow.png
+    :alt: A downward arrow
+
+while True:
+
+
+
+Experimenting with the and and or Operators
+~~~~~~~~~~
+
+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 Boolean operators. The next one is the 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 sentence "Cats have whiskers or dogs have wings." is true. Even though
+dogs don't have wings, when we say "or" we mean that one of the two parts is
+true. The sentence "Cats have whiskers or dogs have tails." is also true.
+(Most of the time when we say "this or that", we mean one thing is true but
+the other thing is false. In programming, "or" means that either of the
+things are true, or maybe both of the things are true.)
+
+Try typing the following into the interactive shell:
+
+>>> True or True
+True
+>>> True or False
+True
+>>> False or True
+True
+>>> False or False
+False
+
+
+
+Experimenting with the not Operator
+~~~~~~~~~
+
+The third Boolean operator is 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 syntax error.
+
+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.
+
+
+Truth Tables
+~~~~~~~~~~~~
+
+If you ever forget how the Boolean operators work, you can look at these
+charts, which are called truth tables:
+
+Table 6-1: The and operator's truth table.
+AandBisEntire statement
+TrueandTrueisTrue
+TrueandFalseisFalse
+FalseandTrueisFalse
+FalseandFalseisFalse
+
+> <table class='simplefulltable centertable' style='width: 300px; text-align:
+center;'> <caption>Table 6-2: The <span class='m'>or<span> operator's truth
+table.
+AorBisEntire statement
+TrueorTrueisTrue
+TrueorFalseisTrue
+FalseorTrueisTrue
+FalseorFalseisFalse
+
+> <table class='simplefulltable centertable' style='width: 300px; text-align:
+center;'> <caption>Table 6-3: The <span class='m'>not<span> operator's truth
+table.
+not AisEntire statement
+not TrueisFalse
+not FalseisTrue
+
+Getting the Player's Input
+--------------------------
+
+1.      while cave != '1' and cave != '2':
+2.          print('Which cave will you go into? (1 or 2)')
+3.          cave = input()
+
+On line 14 the player is asked to enter which cave they chose to enter by
+typing in 1 or 2 and hitting Enter. Whatever string the player typed will be
+stored in cave. After this code is executed, we jump back to the top of the
+while statement and recheck the condition. Remember that the line was:
+
+If this condition evaluates to True, we will enter the while-block again and
+ask the player for a cave number to enter. But if the player typed in 1 or 2,
+then the cave 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 cave number over and over
+again until the player types in 1 or 2. When the player does that, the while-
+block's condition will be False, and we will jump down past the while-block
+and continue with the program.
+
+
+Return Values
+-------------
+
+1.      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
+cave.
+
+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 guarantees that 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.
+
+
+Variable Scope
+--------------
+
+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 global scope and the local scope.
+
+
+Global Scope and Local Scope
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+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 global scope, and each function has a local
+scope of its own. Scopes are also called namespaces.
+
+Variables defined in the global scope can be read outside and inside
+functions, but can only be modified outside of all functions. Variables
+defined in a function's local scope can only be read or modified inside that
+function.
+
+Specifically, we can read the value of global variables from the local scope,
+but attempting to change the value in a global variable from the local scope
+will leave the global variable unchanged. What Python actually does is create
+a local variable with the **same name** as the global variable. But Python
+will consider these to be two different variables.
+
+Also, global variables cannot be read from a local scope if you modify that
+variable inside the local scope. For example, if you had a variable named
+spam in the global scope but also modified a variable named spam in the local
+scope (say, with an assignment statement) then the name "spam" can **only**
+refer to the local scope variable.
+
+Look at this example to see what happens when you try to change a global
+variable from inside a local scope. Remember that the code in the 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 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
+print(spam) # 42
+
+# Call the funky() function:
+funky()
+
+# The global variable was not changed in funky():
+print(spam)    # 42
+
+
+When run, this code will output the following:
+
+42
+99
+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 assignment statement. When the program first executes the line:
+
+1.      cave = ''
+
+...the variable cave 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
+local scope), the cave variable was forgotten and destroyed. But it will be
+defined again when we call the function a second time because line 12 will be
+executed again.
+
+The important thing to remember is that the value of a variable in the local
+scope is not remembered in between function calls.
+
+
+Defining the checkCave() Function
+~~~~~~~~~
+
+1.  def checkCave(chosenCave):
+
+Now we are defining yet another function named checkCave(). Notice that we
+put the text chosenCave in between the parentheses. The variable names in
+between the parentheses are called parameters.
+
+Remember, for some functions like for the str() or randint(), we would pass
+an argument in between the parentheses:
+
+>>> 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 local scope).
+
+**Parameters are local variables** that get defined when a function is
+called. The value stored in the parameter is the argument that was passed in
+the function call.
+
+
+Parameters
+----------
+
+For example, here is a short program that demonstrates parameters. Imagine we
+had a short program that looked like this:
+
+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, sayHello() 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 local variables that store the arguments. It might be
+easier to just remember that the thing in between the parentheses in the def
+statement is an parameter, and the thing in between the parentheses in the
+function call is an argument.
+
+We could have just used the fizzy variable inside the sayHello() function
+instead of using a parameter. (This is because the local scope can still see
+variables in the global scope.) But then we would have to remember to assign
+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
+global variable fizzy directly. Remember that you can read global variables
+inside of functions, you just can't modify the value stored in the variable.
+
+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
+instead of global variables makes function calling simpler to do, especially
+when our programs are very big and have many functions.
+
+
+Where to Put Function Definitions
+---------------------------------
+
+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()
+
+
+
+Displaying the Game Results
+---------------------------
+
+Back to the game's source code:
+
+1.      print('You approach the cave...')
+2.      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 Realm game, we also imported the 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.
+
+1.      print('It is dark and spooky...')
+2.      time.sleep(2)
+
+Here we print some more text and wait again for another 2 seconds. These
+short pauses add suspense to the game, instead of displaying all the text all
+at once. In our jokes program, we called the input() function to wait until
+the player pressed the Enter key. Here, the player doesn't have to do
+anything at all except wait.
+
+1.      print('A large dragon jumps out in front of you! He opens his
+    jaws and...')
+2.      print()
+3.      time.sleep(2)
+
+What happens next? And how does the program decide what happens?
+
+
+Deciding Which Cave has the Friendly Dragon
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+1.      friendlyCave = random.randint(1, 2)
+
+Now we are going to have the program randomly chose which cave had the
+friendly dragon in it. Our call to the random.randint() function will return
+either the integer 1 or the integer 2, and store this value in a variable
+called friendlyCave.
+
+1.      if chosenCave == str(friendlyCave):
+2.          print('Gives you his treasure!')
+
+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. But wait, the value
+in 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.
+
+1.      else:
+2.          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.
+
+
+The Colon :
+-----------
+
+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.
+
+
+Where the Program Really Begins
+-------------------------------
+
+1.  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 assignment
+statement before the variable can be used in the program.
+
+1.  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.
+
+
+Calling the Functions in Our Program
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+1.      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.
+
+1.      caveNumber = chooseCave()
+
+This line also calls a function that we created. Remember that the
+chooseCave() function lets the player type in the cave they choose to go
+into. When the return cave line in this function executes, the program
+execution jumps back down here, and the parameter cave'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.
+
+1.      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 cave the
+player chose to go in.
+
+
+Asking the Player to Play Again
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+1.      print('Do you want to play again? (yes or no)')
+2.      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: 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 Realm game, we used a lot
+of what we learned in the "Guess the Number" game and picked up a few new
+tricks as well. If you didn't understand some of the concepts in this
+program, then read the summary at the end of this chapter, or go over each
+line of the source code again, or try changing the source code and see how
+the program changes. In the next chapter we won't create a game, but learn
+how to use a feature of IDLE called the debugger. The debugger will help us
+figure out what is going on in our program as it is running.
+
+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`_.
+
+
+Designing the Program
+---------------------
+
+Dragon Realm was a pretty simple game. The other games in this book will be a
+bit more complicated. It sometimes helps to write down everything you want
+your game or program to do before you start writing code. This is called
+"designing the program."
+
+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 Realm.
+
+.. image:: images/6-2.png
+
+Figure 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.
+
+
+Summary
+-------
+
+In the "Dragon Realm" game, we created our own functions that the main
+section of the program called. You can think of functions as mini-programs
+within our program. The code inside the function is run when our program
+calls that function. By breaking up our code into functions, we can organize
+our code into smaller and easier to understand sections. We can also run the
+same code by placing it inside of a function, instead of typing it out each
+time we want to run that code.
+
+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 local scope, and variables created outside of all
+functions exist in the global scope. Code in the global scope can not make
+use of local variables. If a local variable has the same name as a variable
+in the global scope, Python considers it to be a separate variable and
+assigning new values to the local variable will not change the value in the
+global variable.
+
+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 local scope, we can be sure that
+the code in one function will not cause bugs in other functions.
+
+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.
+
+`Go to Chapter 5 - Jokes`_`Go to Chapter 7 - Using the Debugger`_
+
+`.. image:: images/buyad.png
+`_
+
+.. _Go to Chapter 5 - Jokes: chapter5.html
+.. _Go to Chapter 7 - Using the Debugger: chapter7.html
+.. _ : http://www.amazon.com/Invent-Your-Computer-Games-
+    Python/dp/0982106017/
+.. _http://inventwithpython.com/chapter6:
+    http://inventwithpython.com/chapter6
+.. _http://inventwithpython.com/dragon.py:
+    http://inventwithpython.com/dragon.py
+.. _http://inventwithpython.com/diff: http://inventwithpython.com/diff
+.. _al@inventwithpython.com: mailto:al@inventwithpython.com
+.. _http://inventwithpython.com/traces/dragon.html:
+    http://inventwithpython.com/traces/dragon.html

guess_the_numbers.rst

+`Go to Chapter 3 - Strings`_`Go to Chapter 5 - Jokes`_
+
+`.. image:: images/buyad.png
+`_
+
+.. image:: images/chap4.png
+
+
+
+Topics Covered In This Chapter:
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+-   import statements
+-   Modules
+-   Arguments
+-   while statements
+-   Conditions
+-   Blocks
+-   Booleans
+-   Comparison operators
+-   The difference between = and ==.
+-   if statements
+-   The break keyword.
+-   The str() and int() functions.
+-   The random.randint() function.
+
+
+The "Guess the Number" Game
+---------------------------
+
+We are going to make a "Guess the Number" game. In this game, the computer
+will think of a random number from 1 to 20, and ask you to guess the number.
+You only get six guesses, but the computer will tell you if your guess is too
+high or too low. If you guess the number within six tries, you win.
+
+This is a good game for you to start with because it uses random numbers,
+loops, and input from the user in a fairly short program. As you write this
+game, you will learn how to convert values to different data types (and why
+you would need to do this).
+
+Because this program is a game, we'll call the user the player, but the word
+"user" would be correct too.
+
+
+Sample Run of "Guess the Number"
+--------------------------------
+
+Here is what our game will look like to the player when the program is run.
+The text that the player types in is in **bold**.
+
+Hello! What is your name?
+**Albert**
+Well, Albert, I am thinking of a number between 1 and 20.
+Take a guess.
+**10**
+Your guess is too high.
+Take a guess.
+**2**
+Your guess is too low.
+Take a guess.
+**4**
+Good job, Albert! You guessed my number in 3 guesses!
+
+
+Enter this code exactly as it appears here, and then save it by clicking on
+the File menu and then Save As. Give it a file name like guess.py then run it
+by pressing the F5 key. Don't worry if you don't understand the code now,
+I'll explain it step by step.
+
+
+Guess the Number's Source Code
+------------------------------
+
+Here is the source code for our Guess the Number game. When you enter this
+code into the file editor, be sure to pay attention to the spacing at the
+front of some of the lines. Some lines have four or eight spaces in front of
+them. After you have typed in the code, save the file as guess.py. You can
+run the program from the file editor by pressing F5. If you see an error
+message, check that you have typed the program in exactly as written.
+
+If you don't want to type all this code, you can download it from this book's
+website at the URL `http://inventwithpython.com/chapter4`_.
+
+**Important Note!** Be sure to run this program with Python 3, and not Python
+2. The programs in this book use Python 3, and you'll get errors if you try
+to run them with Python 2. You can click on Help and then About IDLE to find
+out what version of Python you have.
+
+guess.py
+This code can be downloaded from `http://inventwithpython.com/guess.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.  # This is a guess the number game.
+2.  import random
+3.  4.  guessesTaken = 0
+5.  6.  print('Hello! What is your name?')
+7.  myName = input()
+8.  9.  number = random.randint(1, 20)
+10. print('Well, ' + myName + ', I am thinking of a number between 1 and
+    20.')
+11. 12. while guessesTaken < 6:
+13.     print('Take a guess.') # There are four spaces in front of print.
+14.     guess = input()
+15.     guess = int(guess)
+16. 17.     guessesTaken = guessesTaken + 1
+18. 19.     if guess < number:
+20.         print('Your guess is too low.') # There are eight spaces in
+    front of print.
+21. 22.     if guess > number:
+23.         print('Your guess is too high.')
+24. 25.     if guess == number:
+26.         break
+27. 28. if guess == number:
+29.     guessesTaken = str(guessesTaken)
+30.     print('Good job, ' + myName + '! You guessed my number in ' +
+    guessesTaken + ' guesses!')
+31. 32. if guess != number:
+33.     number = str(number)
+34.     print('Nope. The number I was thinking of was ' + number)
+
+Even though we are entering our source code into a new file editor window, we
+can return to the shell to enter individual instructions in order to see what
+they do. The interactive shell is very good for experimenting with different
+instructions when we are not running a program. You can return to the
+interactive shell by clicking on its window or on its taskbar button. In
+Windows or Mac OS X, the taskbar or dock is on the bottom of the screen. On
+Linux the taskbar may be located along the top of the screen.
+
+If the program doesn't seem to work after you've typed it, check to see if
+you have typed the code exactly as it appears in this book. You can also copy
+and paste your code to the online "diff" tool at
+`http://inventwithpython.com/diff`_. The diff tool will show you how your
+code is different from the source code in this book. In the file editor,
+press Ctrl-A to "Select All" the text you've typed, then press Ctrl-C to copy
+the text to the clipboard. Then, paste this text by clicking in the diff
+tool's text field on the website and click the "Compare" button. The website
+will show you any differences between your code and the code in this book.
+
+There is a diff tool for each program in this book on the
+`http://inventwithpython.com`_ website. A video tutorial of how to use the
+diff tool is available from this book's website at
+`http://inventwithpython.com/videos/`_.
+
+
+The import Statement
+----------
+
+Let's look at each line of code in turn to see how this program works.
+
+1.  # This is a guess the number game.
+
+This line is a comment. Comments were introduced in our Hello World program
+in Chapter 3. Remember that Python will ignore everything after the # sign.
+This just reminds us what this program does.
+
+1.  import random
+
+This is an import statement. Statements are not functions (notice that
+neither import nor random has parentheses after its name). Remember,
+statements are instructions that perform some action but do not evaluate to a
+value. You have already seen statements: assignment statements store a value
+into a variable (but the statement does not evaluate to anything).
+
+While Python includes many built-in functions, some functions exist in
+separate programs called modules. Modules are Python programs that contain
+additional functions. We use the functions of these modules by bringing them
+into our programs with the import statement. In this case, we're importing
+the module random.
+
+The import statement is made up of the import keyword followed by the module
+name. Together, the keyword and module name make up the statement. Line 2
+then is an import statement that imports the module named random which
+contains several functions related to random numbers. (We'll use one of these
+functions later to have the computer come up with a random number for us to
+guess.)
+
+1.  guessesTaken = 0
+
+This line creates a new variable named guessesTaken. We'll store the number
+of guesses the player makes in this variable. Since the player hasn't made
+any guesses so far, we store the integer 0 here.
+
+1.  print('Hello! What is your name?')
+2.  myName = input()
+
+Lines 6 and 7 are the same as the lines in the Hello World program that we
+saw in Chapter 3. Programmers often reuse code from their other programs when
+they need the program to do something that they've already coded before.
+
+Line 6 is a function call to the print() function. Remember that a function
+is like a mini-program that our program runs, and when our program calls a
+function it runs this mini-program. The code inside the print() function
+displays the string you passed it inside the parentheses on the screen.
+
+When these two lines finish executing, the string that is the player's name
+will be stored in the myName variable. (Remember, the string might not really
+be the player's name. It's just whatever string the player typed in.
+Computers are dumb and just follow their programs no matter what.)
+
+
+The random.randint() Function
+---------
+
+1.  number = random.randint(1, 20)
+
+In Line 9 we call a new function named randint(), and then store the return
+value in a variable named number. Remember that function calls are
+expressions because they evaluate to a value. We call this value the function
+call's return value.
+
+Because the randint() function is provided by the random module, we precede
+it with random. (don't forget the period!) to tell our program that the
+function randint() is in the random module.
+
+The randint() function will return a random integer between (and including)
+the two integers we give it. Here, we give it the integers 1 and 20 between
+the parentheses that follow the function name (separated by a comma). The
+random integer that randint() returns is stored in a variable named number;
+this is the secret number the player is trying to guess.
+
+Just for a moment, go back to the interactive shell and enter import random
+to import the random module. Then enter random.randint(1, 20) to see what the
+function call evaluates to. It should return an integer between 1 and 20. Now
+enter the same code again and the function call will probably return a
+different integer. This is because each time the randint() function is
+called, it returns some random number, just like when you roll dice you will
+get a random number each time.
+
+>>> import random
+>>> random.randint(1, 20)
+12
+>>> random.randint(1, 20)
+18
+>>> random.randint(1, 20)
+3
+>>> random.randint(1, 20)
+18
+>>> random.randint(1, 20)
+7
+>>>
+
+
+Whenever we want to add randomness to our games, we can use the randint()
+function. And we use randomness in most games. (Think of how many board games
+use dice.)
+
+You can also try out different ranges of numbers by changing the arguments.
+For example, enter random.randint(1, 4) to only get integers between 1 and 4
+(including both 1 and 4). Or try random.randint(1000, 2000) to get integers
+between 1000 and 2000. Below is an example of calling the random.randint()
+function and seeing what values it returns. The results you get when you call
+the random.randint() function will probably be different (it is random, after
+all).
+
+>>> random.randint(1, 4)
+3
+>>> random.randint(1, 4)
+4
+>>> random.randint(1000, 2000)
+1294
+>>> random.randint(1000, 2000)
+1585
+>>>
+
+
+We can change the game's code slightly to make the game behave differently.
+Try changing line 9 and 10 from this:
+
+1.  number = random.randint(1, **20**)
+2.  print('Well, ' + name + ', I am thinking of a number between 1 and
+    **20**.')
+
+into these lines:
+
+1.  number = random.randint(1, **100**)
+2.  print('Well, ' + name + ', I am thinking of a number between 1 and
+    **100**.')
+
+And now the computer will think of an integer between 1 and 100. Changing
+line 9 will change the range of the random number, but remember to change
+line 10 so that the game also tells the player the new range instead of the
+old one.
+
+
+Calling Functions that are Inside Modules
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+By the way, be sure to enter random.randint(1, 20) and not just randint(1,
+20), or the computer will not know to look in the random module for the
+randint() function and you'll get an error like this:
+
+>>> randint(1, 20)
+Traceback (most recent call last):
+  File "<stdin>", line 1, in <module>
+NameError: name 'randint' is not defined
+>>>
+
+
+Remember, your program needs to run import random before it can call the
+random.randint() function. This is why import statements usually go at the
+beginning of the program.
+
+
+Passing Arguments to Functions
+------------------------------
+
+The integer values between the parentheses in the random.randint(1, 20)
+function call are called arguments. Arguments are the values that are passed
+to a function when the function is called. Arguments tell the function how to
+behave. Just like the player's input changes how our program behaves,
+arguments are inputs for functions.
+
+Some functions require that you pass them values when you call them. For
+example, look at these function calls:
+
+input()
+print('Hello')
+random.randint(1, 20)
+
+
+The input() function has no arguments but the print() function call has one
+and the randint() function call has two. When we have more than one argument,
+we separate each with commas, as you can see in this example. Programmers say
+that the arguments are delimited (that is, separated) by commas. This is how
+the computer knows where one value ends and another begins.
+
+If you pass too many or too few arguments in a function call, Python will
+display an error message, as you can see below. In this example, we first
+called randint() with only one argument (too few), and then we called
+randint() with three arguments (too many).
+
+>>> random.randint(1)
+Traceback (most recent call last):
+  File "<pyshell#1>", line 1, in <module>
+random.randint(1)
+TypeError: randint() takes exactly 3 positional arguments (2 given)
+>>> random.randint(1, 2, 3)
+Traceback (most recent call last):
+  File "<pyshell#2>", line 1, in <module>
+random.randint(1, 2, 3)
+TypeError: randint() takes exactly 3 positional arguments (4 given)
+>>>
+
+
+Notice that the error message says we passed 2 and 4 arguments instead of 1
+and 3. This is because Python always passes an extra, invisible argument.
+This argument is beyond the scope of this book, and you don't have to worry
+about it.
+
+
+Welcoming the Player
+--------------------
+
+Lines 10 and 12 greets the player and tells them about the game, and then
+starts letting the player guess the secret number. Line 10 is fairly simple,
+but line 12 introduces a useful concept called a loop.
+
+1.  print('Well, ' + myName + ', I am thinking of a number between 1 and
+    20.')
+
+In Line 10 the print() function welcomes the player by name, and tells them
+that the computer is thinking of a random number.
+
+But wait - didn't I say that the print() function takes only one string? It
+may look like there's more than one string there. But look at the line
+carefully. The plus signs concatenate the three strings to evaluate down to
+one string, and that is the one string the print() function prints. It might
+look like the commas are separating the strings, but if you look closely you
+see that the commas are *inside* the quotes, and part of the strings
+themselves.
+
+
+Loops
+-----
+
+Line 12 has something called a while statement, which indicates the beginning
+of a while loop. Loops are parts of code that are executed over and over
+again. But before we can learn about while loops, we need to learn a few
+other concepts first. Those concepts are blocks, Booleans, comparison
+operators, conditions, and finally, the while statement.
+
+
+Blocks
+------
+
+A block is one or more lines of code grouped together with the same minimum
+amount of indentation. You can tell where a block begins and ends by looking
+at the line's indentation (that is, the number of spaces in front of the
+line).
+
+A block begins when a line is indented by four spaces. Any following line
+that is also indented by four spaces is part of the block. A block within a
+block begins when a line is indented with another four spaces (for a total of
+eight spaces in front of the line). The block ends when there is a line of
+code with the same indentation before the block started.
+
+Below is a diagram of the code with the blocks outlined and numbered. The
+spaces have black squares filled in to make them easier to count.
+
+.. image:: images/4-1.png
+
+Figure 4-1: Blocks and their indentation. The black dots represent spaces.
+
+For example, look at the code in Figure 4-1. The spaces have been replaced
+with dark squares to make them easier to count. Line 12 has an indentation of
+zero spaces and is not inside any block. Line 13 has an indentation of four
+spaces. Since this indentation is larger than the previous line's
+indentation, we can tell that a new block has started. Lines 14, 15, 17 and
+19 also have four spaces for indentation. Both of these lines have the same
+amount of indentation as the previous line, so we know they are in the same
+block. (We do not count blank lines when we look for indentation.)
+
+Line 20 has an indentation of eight spaces. Eight spaces is more than four
+spaces, so we know a new block has started. This is a block that is inside of
+another block.
+
+Line 22 only has four spaces. The line before line 22 had a larger number of
+spaces. Because the indentation has decreased, we know that block has ended.
+Line 22 is in the same block as the other lines with four spaces.
+
+Line 23 increases the indentation to eight spaces, so again a new block has
+started.
+
+To recap, line 12 is not in any block. Lines 13 to 23 all in one block
+(marked with the circled 1). Line 20 is in a block in a block (marked with a
+circled 2). And line 23 is the only line in another block in a block (marked
+with a circled 3).
+
+When you type code into IDLE, each letter is the same width. You can count
+the number of letters above or below the line to see how many spaces you have
+put in front of that line of code.
+
+In this figure, the lines of code inside box 1 are all in the same block, and
+blocks 2 and 3 are inside block 1. Block 1 is indented with at least four
+spaces from the left margin, and blocks 2 and 3 are indented eight spaces
+from the left margin. A block can contain just one line. Notice that blocks 2
+and 3 are only one line each.
+
+
+The Boolean Data Type
+---------------------
+
+The Boolean data type has only two values: True or False. These values are
+case-sensitive and they are not string values; in other words, you do **not**
+put a ' quote character around them. We will use Boolean values (also called
+bools) with comparison operators to form conditions. (Explained next.)
+
+
+Comparison Operators
+--------------------
+
+In line 12 of our program, the line of code containing the while statement:
+
+1.  while guessesTaken < 6:
+
+The expression that follows the while keyword (guessesTaken < 6) contains two
+values (the value in the variable guessesTaken, and the integer value 6)
+connected by an operator (the < sign, the "less than" sign). The < sign is
+called a comparison operator.
+
+The comparison operator is used to compare two values and evaluate to a True
+or False Boolean value. A list of all the comparison operators is in Table
+4-1.
+
+
+
+
+
+
+Table 4-1: Comparison operators.
+Operator SignOperator Name
+<Less than
+>Greater than
+<=Less than or equal to
+>=Greater than or equal to
+==Equal to
+!=Not equal to
+
+Conditions
+----------
+
+A condition is an expression that combines two values with a comparison
+operator (such as < or >) and evaluates to a Boolean value. A condition is
+just another name for an expression that evaluates to True or False. You'll
+find a list of other comparison operators in Table 4-1.
+
+Conditions always evaluate to a Boolean value: either True or False. For
+example, the condition in our code, guessesTaken < 6 asks "is the value
+stored in guessesTaken less than the number 6?" If so, then the condition
+evaluates to True. If not, the condition evaluates to False.
+
+In the case of our Guess the Number program, in line 4 we stored the value 0
+in guessesTaken. Because 0 is less than 6, this condition evaluates to the
+Boolean value of True. Remember, a condition is just a name for an expression
+that uses comparison operators such as < or !=.
+
+
+Experiment with Booleans, Comparison Operators, and Conditions
+--------------------------------------------------------------
+
+Enter the following expressions in the interactive shell to see their Boolean
+results:
+
+>>> 0 < 6
+True
+>>> 6 < 0
+False
+>>> 50 < 10
+False
+>>> 10 < 11
+True
+>>> 10 < 10
+False
+
+
+The condition 0 < 6 returns the Boolean value True because the number 0 is
+less than the number 6. But because 6 is not less than 0, the condition 6 < 0
+evaluates to False. 50 is not less than 10, so 50 < 10 is False. 10 is less
+than 11, so 10 < 11 is True.
+
+But what about 10 < 10? Why does it evaluate to False? It is False because
+the number 10 is not smaller than the number 10. They are exactly the same
+size. If a girl named Alice was the same height as a boy named Bob, you
+wouldn't say that Alice is taller than Bob or that Alice is shorter than Bob.
+Both of those statements would be false.
+
+Try entering some conditions into the shell to see how these comparison
+operators work:
+
+>>> 10 == 10
+True
+>>> 10 == 11
+False
+>>> 11 == 10
+False
+>>> 10 != 10
+False
+>>> 10 != 11
+True
+>>> 'Hello' == 'Hello'
+True
+>>> 'Hello' == 'Good bye'
+False
+>>> 'Hello' == 'HELLO'
+False
+>>> 'Good bye' != 'Hello'
+True
+
+
+Notice the difference between the assignment operator (=) and the "equal to"
+comparison operator (==). The equal (=) sign is used to assign a value to a
+variable, and the equal to (==) sign is used in expressions to see whether
+two values are equal. It's easy to accidentally use one when you meant to use
+the other, so be careful of what you type in.
+
+Two values that are different data types will **always** be not equal to each
+other. For example, try entering the following into the interactive shell:
+
+>>> 42 == 'Hello'
+False
+>>> 42 != '42'
+False
+
+
+
+Looping with while Statements
+-----------
+
+The while statement marks the beginning of a loop. Sometimes in our programs,
+we want the program to do something over and over again. When the execution
+reaches a while statement, it evaluates the condition next to the while
+keyword. If the condition evaluates to True, the execution moves inside the
+while-block. (In our program, the while-block begins on line 13.) If the
+condition evaluates to False, the execution moves all the way past the while-
+block. (In our program, the first line after the while-block is line 28.)
+
+A while statement always has a colon (the : sign) after the condition.
+
+1.  while guessesTaken < 6:
+
+.. image:: images/4-2.png
+
+Figure 4-2: The while loop's condition.
+
+Figure 4-2 shows how the execution flows depending on the condition. If the
+condition evaluates to True (which it does the first time, because the value
+of guessesTaken is 0), execution will enter the while-block at line 13 and
+keep going down. Once the program reaches the end of the while-block, instead
+of going down to the next line, it jumps back up to the while statement's
+line (line 12). It then re-evaluates the condition, and if it still evaluates
+to True we enter the while-block again.
+
+This is how the loop works. As long as the condition is True, the program
+keeps executing the code inside the while-block repeatedly until we reach the
+end of the while-block and the condition is False. And, until guessesTaken is
+equal to or greater than 6, we will keep looping.
+
+Think of the while statement as saying, "while this condition is true, keep
+looping through the code in this block".
+
+You can make this game harder or easier by changing the number of guesses the
+player gets. All you have to do is change this line:
+
+1.  while guessesTaken < **6**:
+
+into this line:
+
+1.  while guessesTaken < **4**:
+
+...and now the player only gets four guesses instead of six guesses. By
+setting the condition to guessesTaken < 4, we ensure that the code inside the
+loop only runs four times instead of six. This makes the game much more
+difficult. To make the game easier, set the condition to guessesTaken < 8 or
+guessesTaken < 10, which will cause the loop to run a few more times than
+before and accept more guesses from the player.
+
+Of course, if we removed line 17 (guessesTaken = guessesTaken + 1) altogether
+then the guessesTaken would never increase and the condition would always be
+True. This would give the player an unlimited number of guesses.
+
+
+The Player Guesses
+------------------
+
+Lines 13 to 17 ask the player to guess what the secret number is and lets
+them enter their guess. We store this guess in a variable, and then convert
+that string value into an integer value.
+
+1.      print('Take a guess.') # There are four spaces in front of print.
+2.      guess = input()
+
+The program now asks us for a guess. We type in our guess and that number is
+stored in a variable named guess.
+
+
+Converting Strings to Integers with the int() Function
+~~~~~~~~~
+
+1.      guess = int(guess)
+
+In line 15, we call a new function called int(). The int() function takes one
+argument. The input() function returned a string of text that player typed.
+But in our program, we will want an integer, not a string. If the player
+enters 5 as their guess, the input() function will return the string value
+'5' and not the integer value 5. Remember that Python considers the string
+'5' and the integer 5 to be different values. So the int() function will take
+the string value we give it and return the integer value form of it.
+
+Let's experiment with the int() function in the interactive shell. Try typing
+the following:
+
+>>> int('42')
+42
+>>> int(42)
+42
+>>> int('hello')
+
+Traceback (most recent call last):
+  File "<pyshell#4>", line 1, in <module>
+int('forty-two')
+ValueError: invalid literal for int() with base 10: 'hello'
+>>> int('forty-two')
+
+Traceback (most recent call last):
+  File "<pyshell#5>", line 1, in <module>
+int('forty-two')
+ValueError: invalid literal for int() with base 10: 'forty-two'
+>>> int(' 42 ')
+42
+>>> 3 + int('2')
+5
+
+
+We can see that the int('42') call will return the integer value 42, and that
+int(42) will do the same (though it is kind of pointless to convert an
+integer to an integer). However, even though you can pass a string to the
+int() function, you cannot just pass any string. For example, passing 'hello'
+to int() (like we do in the int('hello') call) will result in an error. The
+string we pass to int() must be made up of numbers.
+
+The integer we pass to int() must also be numerical, rather than text, which
+is why int('forty-two') also produces an error. That said, the int() function
+is slightly forgiving; if our string has spaces on either side, it will still
+run without error. This is why the int(' 42 ') call works.
+
+The 3 + int('2') line shows an expression that adds an integer 3 to the
+return value of int('2') (which is the integer 2). The expression evaluates
+to 3 + 2, which then evaluates to 5. So even though we cannot add an integer
+and a string (3 + '2' would show us an error), we can add an integer to a
+string that has been converted to an integer.
+
+Remember, back in our program on line 15 the guess variable originally held
+the string value of what the player typed. We will overwrite the string value
+stored in guess with the integer value returned by the int() function. This
+is because we will later compare the player's guess with the random number
+the computer came up with. We can only compare two integer values to see if
+one is greater (that is, higher) or less (that is, lower) than the other. We
+cannot compare a string value with an integer value to see if one is greater
+or less than the other, even if that string value is numeric such as '5'.
+
+In our Guess the Number game, if the player types in something that is not a
+number, then the function call int() will result in an error and the program
+will crash. In the other games in this book, we will add some more code to
+check for error conditions like this and give the player another chance to
+enter a correct response.
+
+Notice that calling int(guess) does not change the value in the guess
+variable. The code int(guess) is an expression that evaluates to the integer
+value form of the string stored in the guess variable. We must assign this
+return value to guess in order to change the value in guess to an integer
+with this full line: guess = int(guess)
+
+
+Incrementing Variables
+~~~~~~~~~~~~~~~~~~~~~~
+
+1.      guessesTaken = guessesTaken + 1
+
+Once the player has taken a guess, we want to increase the number of guesses
+that we remember the player taking.
+
+The first time that we enter the loop block, guessesTaken has the value of 0.
+Python will take this value and add 1 to it. 0 + 1 is 1. Then Python will
+store the new value of 1 to guessesTaken.
+
+Think of line 17 as meaning, "the guessesTaken variable should be one more
+than what it already is".
+
+When we add one to an integer value, programmers say they are incrementing
+the value (because it is increasing by one). When we subtract one from a
+value, we are decrementing the value (because it is decreasing by one). The
+next time the loop block loops around, guessesTaken will have the value of 1
+and will be incremented to the value 2.
+
+
+if Statements
+-----------
+
+
+Is the Player's Guess Too Low?
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Lines 19 and 20 check if the number that the player guessed is less than the
+secret random number that the computer came up with. If so, then we want to
+tell the player that their guess was too low by printing this message to the
+screen.
+
+1.      if guess < number:
+2.           print('Your guess is too low.') # There are eight spaces in
+    front of print.
+
+Line 19 begins an if statement with the keyword, if. Next to the if keyword
+is the condition. Line 20 starts a new block (you can tell because the
+indentation has increased from line 19 to line 20.) The block that follows
+the if keyword is called an if-block. An if statement is used if you only
+want a bit of code to execute if some condition is true. Line 19 has an if
+statement with the condition guess < number. If the condition evaluates to
+True, then the code in the if-block is executed. If the condition is False,
+then the code in the if-block is skipped.
+
+.. image:: images/4-3.png
+
+Figure 4-3: if and while statements.
+
+Like the while statement, the if statement also has a keyword, followed by a
+condition, a colon, and then a block of code. See Figure 4-3 for a comparison
+of the two statements.
+
+
+
+The if statement works almost the same way as a while statement, too. But
+unlike the while-block, execution does not jump back to the if statement at
+the end of the if-block. It just continues on down to the next line. In other
+words, if statements won't loop.
+
+
+
+If the condition is True, then all the lines inside the if-block are
+executed. The only line inside this if-block on line 19 is a print() function
+call.
+
+
+
+
+
+
+
+
+
+
+
+
+If the integer the player enters is less than the random integer the computer
+thought up, the program displays Your guess is too low. If the integer the
+player enters is equal to or larger than the random integer (in which case,
+the condition next to the if keyword would have been False), then this block
+would have been skipped over.
+
+
+Is the Player's Guess Too High?
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Lines 22 to 26 in our program check if the player's guess is either too big
+or exactly equal to the secret number.
+
+1.      if guess > number:
+2.           print('Your guess is too high.')
+
+If the player's guess is larger than the random integer, we enter the if-
+block that follows the if statement. The print() line tells the player that
+their guess is too big.
+
+
+Leaving Loops Early with the break Statement
+----------
+
+1.      if guess == number:
+2.           break
+
+This if statement's condition checks to see if the guess is equal to the
+random integer. If it is, we enter line 26, the if-block that follows it.
+
+The line inside the if-block is a break statement that tells the program to
+immediately jump out of the while-block to the first line after the end of
+the while-block. (The break statement does not bother re-checking the while
+loop's condition, it just breaks out immediately.)
+
+The break statement is just the break keyword by itself, with no condition or
+colon.
+
+If the player's guess is not equal to the random integer, we do not break out
+of the while-block, we will reach the bottom of the while-block anyway. Once
+we reach the bottom of the while-block, the program will loop back to the top
+and recheck the condition (guessesTaken < 6). Remember after the guessesTaken
+= guessesTaken + 1 line of code executed, the new value of guessesTaken is 1.
+Because 1 is less than 6, we enter the loop again.
+
+If the player keeps guessing too low or too high, the value of guessesTaken
+will change to 2, then 3, then 4, then 5, then 6. If the player guessed the
+number correctly, the condition in the if guess == number statement would be
+True, and we would have executed the break statement. Otherwise, we keep
+looping. But when guessesTaken has the number 6 stored, the while statement's
+condition is False, since 6 is not less than 6. Because the while statement's
+condition is False, we will not enter the loop and instead jump to the end of
+the while-block.
+
+The remaining lines of code run when the player has finished guessing (either
+because the player guessed the correct number, or because the player ran out
+of guesses). The reason the player exited the previous loop will determine if
+they win or lose the game, and the program will display the appropriate
+message on the screen for either case.
+
+
+Check if the Player Won
+-----------------------
+
+1.  if guess == number:
+
+Unlike the code in line 25, this line has no indentation, which means the
+while-block has ended and this is the first line outside the while-block.
+When we left the while block, we did so either because the while statement's
+condition was False (when the player runs out of guesses) or if we executed
+the break statement (when the player guesses the number correctly). With line
+28, check again to see if the player guessed correctly. If so, we enter the
+if-block that follows.
+
+1.      guessesTaken = str(guessesTaken)
+2.      print('Good job, ' + myName + '! You guessed my number in ' +
+    guessesTaken + ' guesses!')
+
+Lines 29 and 30 are inside the if-block. They only execute if the condition
+in the if statement on line 28 was True (that is, if the player correctly
+guessed the computer's number).
+
+In line 29 we call the new function str(), which returns the string form of
+an argument. We use this function because we want to change the integer value
+in guessesTaken into its string version because we can only use strings in
+calls to print().
+
+Line 29 tells the player that they have won, and how many guesses it took
+them. Notice in this line that we change the guessesTaken value into a string
+because we can only add (that is, concatenate) strings to other strings. If
+we were to try to add a string to an integer, the Python interpreter would
+display an error.
+
+
+Check if the Player Lost
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+1.  if guess != number:
+
+In Line 32, we use the comparison operator != with the if statement's
+condition to mean "is not equal to." If the value of the player's guess is
+lower or higher than (and therefore, not equal to) the number chosen by the
+computer, then this condition evaluates to True, and we enter the block that
+follows this if statement on line 33.
+
+Lines 33 and 34 are inside the if-block, and only execute if the condition is
+True.
+
+1.      number = str(number)
+2.      print('Nope. The number I was thinking of was ' + number)
+
+In this block, we tell the player what the number is because they failed to
+guess correctly. But first we have to store the string version of number as
+the new value of number.
+
+This line is also inside the if-block, and only executes if the condition was
+True. At this point, we have reached the end of the code, and the program
+terminates.
+
+Congratulations! We've just programmed our first real game!
+
+
+Summary: What Exactly is Programming?
+-------------------------------------
+
+If someone asked you, "What exactly is programming anyway?" what could you
+say to them? Programming is just the action of writing code for programs,
+that is, creating programs that can be executed by a computer.
+
+"But what exactly is a program?" When you see someone using a computer
+program (for example, playing our Guess The Number game), all you see is some
+text appearing on the screen. The program decides what exact text to show on
+the screen (which is called the output), based on its instructions (that is,
+the program) and on the text that the player typed on the keyboard (which is
+called the input). The program has very specific instructions on what text to
+show the user. A program is just a collection of instructions.
+
+"What kind of instructions?" There are only a few different kinds of
+instructions, really.
+
+1.  Expressions, which are made up of values connected by operators.
+    Expressions are all evaluated down to a single value, like 2 + 2
+    evaluates to 4 or 'Hello' + ' ' + 'World' evaluates to 'Hello World'.
+    Function calls are also part of expressions because they evaluate to a
+    single value themselves, and this value can be connected by operators to
+    other values. When expressions are next to the if and while keywords, we
+    also call them conditions.
+2.  Assignment statements, which simply store values in variables so we
+    can remember the values later in our program.
+3.  if, while and break are flow control statements because they decide
+    which instructions are executed. The normal flow of execution for a
+    program is to start at the top and execute each instruction going down
+    one by one. But these flow control statements can cause the flow to skip
+    instructions, loop over instructions, or break out of loops. Function
+    calls also change the flow of execution by jumping to the start of a
+    function.
+4.  The print() function, which displays text on the screen. Also, the
+    input() function can get text from the user through the keyboard. This is
+    called I/O (pronounced like the letters, "eye-oh"), because it deals with
+    the input and output of the program.
+
+And that's it, just those four things. Of course, there are many details
+about those four types of instructions. In this book you will learn about new
+data types and operators, new flow control statements besides if, while and
+break, and several new functions. There are also different types of I/O
+(input from the mouse, and outputting sound and graphics and pictures instead
+of just text.)
+
+For the person using your programs, they really only care about that last
+type, I/O. The user types on the keyboard and then sees things on the screen
+or hears things from the speakers. But for the computer to figure out what
+sights to show and what sounds to play, it needs a program, and programs are
+just a bunch of instructions that you, the programmer, have written.
+
+
+A Web Page for Program Tracing
+------------------------------
+
+If you have access to the Internet and a web browser, you can go to this
+book's website at `http://inventwithpython.com/traces`_ you will find a page
+that traces through each of the programs in this book. By following along
+with the trace line by line, it might become more clear what the Guess the
+Number program does. This website just shows a simulation of what happens
+when the program is run. No actual code is really being executed.
+
+.. image:: images/4-4.png
+
+Figure 4-4: The tracing web page.
+
+.. image:: images/4-4.png
+
+Figure 4-4: The tracing web page.
+
+The left side of the web page shows the source code, and the highlighted line
+is the line of code that is about to be executed. You execute this line and
+move to the next line by clicking the "Next" button. You can also go back a
+step by clicking the "Previous" button, or jump directly to a step by typing
+it in the white box and clicking the "Jump" button.
+
+On the right side of the web page, there are three sections. The "Current
+variable values" section shows you each variable that has been assigned a
+value, along with the value itself. The "Notes" section will give you a hint
+about what is happening on the highlighted line. The "Program output" section
+shows the output from the program, and the input that is sent to the program.
+(This web page automatically enters text to the program when the program
+asks.)
+
+So go to each of these web pages and click the "Next" and "Previous" buttons
+to trace through the program like we did above.
+
+A video tutorial of how to use the online tracing tool is available from this
+book's website at `http://inventwithpython.com/videos/`_.
+
+`Go to Chapter 3 - Strings`_`Go to Chapter 5 - Jokes`_
+
+`.. image:: images/buyad.png
+`_
+
+.. _Go to Chapter 3 - Strings: chapter3.html
+.. _Go to Chapter 5 - Jokes: chapter5.html
+.. _ : http://www.amazon.com/Invent-Your-Computer-Games-
+    Python/dp/0982106017/
+.. _http://inventwithpython.com/chapter4:
+    http://inventwithpython.com/chapter4
+.. _http://inventwithpython.com/guess.py:
+    http://inventwithpython.com/guess.py
+.. _http://inventwithpython.com/diff: http://inventwithpython.com/diff
+.. _al@inventwithpython.com: mailto:al@inventwithpython.com
+.. _http://inventwithpython.com: http://inventwithpython.com
+.. _http://inventwithpython.com/videos/:
+    http://inventwithpython.com/videos/
+.. _http://inventwithpython.com/traces:
+    http://inventwithpython.com/traces

images/.hgkeep

Empty file removed.

images/cover.png

Removed
Old image
+`Go to Chapter 4 - Guess the Number`_`Go to Chapter 6 - Dragon Realm`_
+
+`.. image:: images/buyad.png
+`_
+
+.. image:: images/chap5.png
+
+
+
+Topics Covered In This Chapter:
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+-   Using print()'s end keyword argument to skip newlines.
+-   Escape characters.
+-   Using single quotes and double quotes for strings.
+
+
+Make the Most of print()
+-------
+
+Most of the games in this book will have simple text for input and output.
+The input is typed by the user on the keyboard and entered to the computer.
+The output is the text displayed on the screen. In Python, the print()
+function can be used for displaying textual output on the screen. We've
+learned how the basics of using the print() function, but there is more to
+learn about how strings and print() work in Python.
+
+
+Sample Run of Jokes
+-------------------
+
+What do you get when you cross a snowman with a vampire?
+> <br> Frostbite!
+> <br> What do dentists call an astronaut's cavity?
+> <br> A black hole!
+> <br> Knock knock.
+> <br> Who's there?
+> <br> Interrupting cow.
+> <br> Interrupting cow wh-MOO!
+> <div>
+Joke's Source Code
+------------------
+
+Here is the source code for our short jokes program. Type it into the file
+editor and save it as *jokes.py*. If you do not want to type this code in,
+you can also download the source code from this book's website at the URL
+`http://inventwithpython.com/chapter5`_.
+
+**Important Note!** Be sure to run this program with Python 3, and not Python
+2. The programs in this book use Python 3, and you'll get errors if you try
+to run them with Python 2. You can click on Help and then About IDLE to find
+out what version of Python you have.
+
+jokes.py
+This code can be downloaded from `http://inventwithpython.com/jokes.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.  print('What do you get when you cross a snowman with a vampire?')
+2.  input()
+3.  print('Frostbite!')
+4.  print()
+5.  print('What do dentists call a astronaut\'s cavity?')
+6.  input()
+7.  print('A black hole!')
+8.  print()
+9.  print('Knock knock.')
+10. input()
+11. print("Who's there?")
+12. input()
+13. print('Interrupting cow.')
+14. input()
+15. print('Interrupting cow wh', end='')
+16. print('-MOO!')
+
+Don't worry if you don't understand everything in the program. Just save and
+run the program. Remember, if your program has bugs in it, you can use the
+online diff tool at `http://inventwithpython.com/chapter5`_.
+
+
+How the Code Works
+------------------
+
+Let's look at the code more carefully.
+
+1.  print('What do you get when you cross a snowman with a vampire?')
+2.  input()
+3.  print('Frostbite!')
+4.  print()
+
+Here we have three print() function calls. Because we don't want to tell the
+player what the joke's punch line is, we have a call to the input() function
+after the first print(). The player can read the first line, press Enter, and
+then read the punch line.
+
+The user can still type in a string and hit Enter, but because we aren't
+storing this string in any variable, the program will just forget about it
+and move to the next line of code.
+
+The last print() function call has no string argument. This tells the program
+to just print a blank line. Blank lines can be useful to keep our text from
+being bunched up together.
+
+
+Escape Characters
+-----------------
+
+1.  print('What do dentists call a astronaut\'s cavity?')
+2.  input()
+3.  print('A black hole!')
+4.  print()
+
+In the first print()above, you'll notice that we have a slash right before
+the single quote (that is, the apostrophe). This backslash ( \ is a
+backslash, / is a forward slash) tells us that the letter right after it is
+an escape character. An escape character helps us print out letters that are
+hard to enter into the source code. There are several different escape
+characters, but in our call to print() the escape character is the single
+quote.
+
+We have to have the single quote escape character because otherwise the
+Python interpreter would think that this quote meant the end of the string.
+But we want this quote to be a part of the string. When we print this string,
+the backslash will not show up.
+
+
+Some Other Escape Characters
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+What if you really want to display a backslash? This line of code would not
+work:
+
+>>> print('He flew away in a green\teal helicopter.')
+
+
+That print() function call would show up as:
+
+He flew away in a green    eal helicopter.
+
+
+This is because the "t" in "teal" was seen as an escape character since it
+came after a backslash. The escape character t simulates pushing the Tab key
+on your keyboard. Escape characters are there so that strings can have
+characters that cannot be typed in.
+
+Instead, try this line:
+
+>>> print('He flew away in a green\\teal helicopter.')
+
+
+Here is a list of escape characters in Python:
+
+Table 5-1: Escape Characters
+Escape CharacterWhat Is Actually Printed
+\\Backslash (\)
+\'Single quote (')
+\"Double quote (")
+\nNewline
+\tTab