Game Development Reference
In-Depth Information
4 - Guess the Number
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 topic, 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.
10. 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.
Search WWH ::




Custom Search