Game Development Reference
In-Depth Information
4 - Guess the 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 .
33. number = str(number)
34. 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.
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.
Assignment statements, which simply store values in variables so we can remember the
values later in our program.
if , while and break are flow control statements because they decide which
Search WWH ::




Custom Search