Game Development Reference
In-Depth Information
4 - Guess the Number
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 with comparison operators to form conditions.
(See below.)
Comparison Operators
In line 12 of our program, the line of code containing the while statement:
12. 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.
Search WWH ::




Custom Search