Game Development Reference
In-Depth Information
Table 4-1: Comparison operators.
Operator Sign
Operator 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