Design Patterns and Video Games

Discover Python and Patterns (6): Random

In the previous post, the magic number is always the same. I propose now to introduce random numbers to change the magic number every time we launch the game. I also present imports and more on integers.

This post is part of the Discover Python and Patterns series

Library import

In the previous programs, I used print() and input() functions. They are part of the Python language, so nothing is required to use them. For other functions, like the ones for random number generation, we have to tell Python we want to use them.

For instance, to use random number functions, we need to import the random package at the beginning of our program:

import random

Note that if you type this in Spyder, you see a warning telling that you don't use the random package. It is as expected, and it disappears once you use any function of this package.

The random package is part of the Python standard library, so it is always available when we ask for it. For other libraries, we have to install them in the current Python environment. The default Anaconda setup we installed in the first post already includes a lot of useful libraries. For more specific ones, like PyGame, I'll show you later how to proceed.

Generate a random integer

We can generate a random integer in the following way:

magicNumber = random.randint(1,10)

We use the randint() function inside the random package. To tell that this function is in the random package, we have to type the name of the package before it, followed by a dot.

This line generates a random integer between 1 and 10 and puts it in magicNumber. If you replace the initialization in the program in the previous post with this one, the game now runs with a different magic number every time.

Import a specific function

If you don't want to type the package name before the function, you can use the following syntax:

from random import randint
magicNumber = randint(1,10)

The first line indicates that we want to use the randint() function from the random package. Then, if the randint() function is found in the following lines (of the same file), Python assumes that it is the one of the random package. You can replace the import random with from random import randint at the beginning of the program if randint() is the only function you need.

Import all symbols

Python also offers the following syntax to import all symbols from a package:

from random import *

All functions including randint() are then available without the need to type the package name before. Do not use this syntax, it is very dangerous since no one knows all the symbols of a package. You can have conflicts if two packages use equal symbols. And trust me, if it happens in a large application, I wish you good luck to find it :)

Clues for the player

It is not easy for the player to find the magic number. I propose to give some clues to help him/her. More specifically, I suggest telling if the number entered by the player is higher or lower:

import random

magicNumber = random.randint(1,10)
while True:
    word = input("What is the magic number? ")
    if word == "quit":
        break

    try:
        playerNumber = int(word)
    except ValueError:
        print("Please type a number without decimals!")
        continue

    # Cases
    if playerNumber == magicNumber:
        print("This is correct! You win!")
        break
    elif magicNumber < playerNumber:
        print("The magic number is lower")
    elif magicNumber > playerNumber:
        print("The magic number is higher")

    print("This is not the magic number. Try again!")

I added two more cases in lines 19-22. In lines 19-20 we test if the magic number is lower than the entered number. If it is the case, we print the message "The magic number is lower". Similarly in lines 21-22 for the other case. Note the elif statement I presented before: you must use it after an if statement, and you can add as much as you need. Python executes the block of the first condition evaluated as True, and ignores the other ones.

Count the guesses

At last, I propose to count the guesses as a score, to see if the player was good or not. We need an integer variable to store this count. We initialize it at the beginning of the program:

guessCount = 0

Then, after every player input, we increase this counter by one:

guessCount = guessCount + 1

Python understands most arithmetics expression, here the addition between two numbers: guessCount + 1.

There is a more compact syntax to increment a number:

guessCount += 1

At the end of the game, we display a message with the number of guesses:

print("You guessed the magic number in {} guesses.".format(guessCount))

Python replaces the braces {} with the argument of the format() function following the message. Don't forget the dot between the string literal (everything between the quotes) and the function call format(guessCount).

Final program

# Import the random package
import random

# Generate a random Magic number
magicNumber = random.randint(1,10)
# An integer to count the guesses
guessCount = 0
while True:
    # Player input
    word = input("What is the magic number? ")
    # Quit if the player types "quit"
    if word == "quit":
        break

    # Int casting with exception handling
    try:
        playerNumber = int(word)
    except ValueError:
        print("Please type a number without decimals!")
        continue

    # Increase the number of guesses
    guessCount += 1    

    # Cases
    if playerNumber == magicNumber:
        print("This is correct! You win!")
        print("You guessed the magic number in {} guesses.".format(guessCount))
        break
    elif magicNumber < playerNumber:
        print("The magic number is lower")
    elif magicNumber > playerNumber:
        print("The magic number is higher")

    # Wrong number, continue 
    print("This is not the magic number. Try again!")

In the next post, I'll show you how to better organize your program using functions.