Game Development Reference
In-Depth Information
4 - Guess the Number
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 . Here 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:
9. number = random.randint(1, 20 )
10. print('Well, ' + name + ', I am thinking of a number
between 1 and 20 .')
Search WWH ::




Custom Search