A program with Functions
We have learned a lot of concepts so far, and to make sure we are on track, lets write a program that contains all the knowledge in this chapter combined with previous chapters.
This code is called 'Guess the Number'. This is a very simple program to guess the number selected by the computer at random!
# This is a guess the number game.
import random
secretNumber = random.randint(1, 20)
print('I am thinking of a number between 1 and 20.')
# Ask the player to guess 6 times.
for guessesTaken in range(1, 7):
print('Take a guess.')
guess = int(input())
if guess < secretNumber:
print('Your guess is too low.')
elif guess > secretNumber:
print('Your guess is too high.')
else:
break # This condition is the correct guess!
if guess == secretNumber:
print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
else:
print('Nope. The number I was thinking of was ' + str(secretNumber))
Does the program look too scary? Well, let's start with the output:
I am thinking of a number between 1 and 20.
Take a guess.
1
Your guess is too low.
Take a guess.
2
Your guess is too low.
Take a guess.
3
Your guess is too low.
Take a guess.
4
Your guess is too low.
Take a guess.
20
Your guess is too high.
Take a guess.
19
Your guess is too high.
Nope. The number I was thinking of was 12
What we see is that the program only runs a maximum of 6 times! After that it terminates itself. This can be observed from the for guessesTaken in range(1, 7):
condition.
Lets try another run:
I am thinking of a number between 1 and 20.
Take a guess.
9
Your guess is too low.
Take a guess.
14
Your guess is too low.
Take a guess.
19
Your guess is too high.
Take a guess.
15
Good job! You guessed my number in 4 guesses!
Here it terminates in 4 tries, after the user figures out the number. These statements take care of the termination, by force or by luck:
if guess == secretNumber:
print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
else:
print('Nope. The number I was thinking of was ' + str(secretNumber))
Now, lets scan the program block by block:
# This is a guess the number game.
import random
secretNumber = random.randint(1, 20)
print('I am thinking of a number between 1 and 20.')
Here, we first import random
in order to use the random.randint()
function, as learnt in the last units. Once we store the random number inside a variable named secretNumber
, we start the game.
for guessesTaken in range(1, 7):
print('Take a guess.')
guess = int(input())
if guess < secretNumber:
print('Your guess is too low.')
elif guess > secretNumber:
print('Your guess is too high.')
else:
break # This condition is the correct guess!
This loop runs 6 times, as the range is 1 to 7. We recall that a range functions runs from the first argument to the second argument, ignoring the latter. This means that the user has 6 tries to figure out the number. We take an input by using guess = int(input())
and stores it in the guess
variable.
After this, we check if the guess
is greater, smaller or equal to the secretNumber
. If it is smaller than the secretNumber
, then we print that it's too low. Otherwise, if it is larger than the secretNumber
, then we print that it too high. Now, what if they guess the right number? We break
the loop.
if guess == secretNumber:
print('Good job! You guessed my number in ' + str(guessesTaken) + ' guesses!')
else:
print('Nope. The number I was thinking of was ' + str(secretNumber))
Now, finally moving to the last block. If the guess
is equal to the secretNumber
, then we print that they did a good job, along with the number of chances it took for them to guess it. Otherwise, we show them the number stored in secretNumber
.
With this, we end the chapter on Functions. Let's move on to some questions before diving deeper into advanced concepts!