design, write and debug programs that accomplish specific goals, including controlling or simulating physical systems; solve problems by decomposing them into smaller parts.

use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs

6.1b

LO: Working with random numbers/Using import.

Back in 5.2b we designed a guessing game where the user had to guess the mystery number and the program gave feedback if the guess was too high or too low. One major problem with that game was the fact that we, the programmer, couldn't play it - we had to come up with the mystery number when writing the game. Here is the updated code from 6.2a:


>#declare a flag variable
play = True #we say it is True because we want the game to start

#declare a variable to reference our mystery number
mystery_num = 14

#declare a variable to reference the number of guesses
number_of_guesses = 0

#define a function to call if the user wins
def win():
    print("Well done, you guessed the mystery number!")
    print("The mystery number was", mystery_num)
    print("You guessed the number in", number_of_guesses, "guesses!")
    print("Thanks for playing.")

#define a function to call if the user loses
def lose():
    print("Sorry, you didn't win this time.")

#introduce
print("This game will ask you to guess a mystery number between 0 and 20.")
print("You have six attempts to guess the mystery number!")
print("") #this is just a blank line to make it look clearer

#start the game loop
while play:
    #check how many guesses the user has had
    if number_of_guesses > 5:
        #call the losing function
        lose()
        #break out of the loop
        break
    
    #update the number of guesses by 1
    number_of_guesses = number_of_guesses + 1
    
    #get input
    user_input = input("Enter a number between 0 and 20 :")

    #cast to int
    guess = int(user_input)

    #compare the guess with mystery number
    if guess == mystery_num:
        #call the winning function
        win()
        play = False
    if guess > mystery_num:
        #respond to user
        print("Too big, try again")
    if guess < mystery_num:
        #respond to user
        print("Too small, try again")


			

The problem we have is the variable mystery_num = 14, as we have to declare it we can't have a go at guessing it. To get round this problem we are going to use a random* number generatator - specifically a random* integer generator called randint() from the random module.
If you recall from 6.6a we can import this function using:


from module import a function

#in this case it would be
from random import randint

			

Now we have imported the function we can use it in our variable declaration:

			
#declare a variable to reference our mystery number
mystery_num = randint(1,20)

			

The randint() works just like the range() function we have already used in writing for loops. In this case we have used the argument 1 to specify the start of the range and the argument (it will be equal or greater than) 20 to speciffy the end of the range (it will be equal or less than), the function will randomly pick a number between 1 and 20 every time it is called**.

Have a go at rewriting the guessing game using the randint() function. The complete code can be found in the code example section below as 6_guessingGame_with_random.py









* A computer can't generate random numbers, instead they use pseudorandomness - a sample of numbers that look close to random, but were generated using a deterministic process.

** the function will randomly pick a number between 1 and 20 every time it is called. This is very important! Where you use the function is very important: If you put the function inside a loop it will generate a new random number each time it is called - this can be useful, but also a disaster. If used inside the game loop of the guessing game the function will generate a new random number on each loop - that is to say that after the user has had a guess the number will change!


Code Examples

Resources