Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only. T&Cs apply

Find out more

Practicing functions

Learn algorithms, logic, and Python basics. Create simple programs, grasp computer science fundamentals, and see its impact across various fields.

The aim of this exercise is to improve the previous ‘guess the number’ game to make it better and more intelligent! You will implement functions to make the game better by generating a random secret, providing hints (how far/close the guess is) to the user.

In this activity, you will have the opportunity to practice writing functions in Python.

The problems you need to solve

For this exercise, you will need to do the following:

  1. Call the get_random_secret function that we implemented for you to get a random secret (instead of having a hard-coded one).
  2. Write a function to check the validity of the user input. It should return True if the input is between 0 and 100 (inclusive) or False otherwise (negative number or greater than 100).
  3. Write a function that will estimate how far the guess is from the secret and provide nice messages to the user to guide them.

Prepare

The files you will need to complete this exercise, and the solution to it, can be found in the Supporting files and solutions .zip file. Use the files in the folder ‘lab 6’. 

To get started, open Visual Studio Code on your computer. Then open the functions.py file. You will see a skeleton code with multiple TODO comments to help you complete this exercise. Each TODO block (they appear as Python comments with # and the keyword TODO) is a small sub-task for you. 

Complete the code

Have a look at all the TODO comments and complete the code required for each.
If you get stuck, please have a look at the solution for a hint.
Running the code: Open a terminal (select Terminal → New Terminal from the menu bar on the top) and type python functions.py in the terminal pane (and hit the ENTER key) to run the program.

Example output (your output may differ since the secret and user input will be different):

> python functions.py
Please provide a guess: -10
Please provide a number within the range [0, 100].
Please provide a guess: 10
Cold
Please provide a guess: 90
Cold
Please provide a guess: 30
Hot
Please provide a guess: 50
Very hot!
Please provide a guess: 60
Hot
Please provide a guess: 55
Very cold
Please provide a guess: 45
Congrats! The secret was 45. You found the secret after 7 attempts.

Solutions

Don’t forget, there is usually more than one solution. If you thought of something else, it’s not necessarily wrong. If your solution works, that’s great. Read my code anyway – a good programmer is one that reads and learns from others’ code!

This article is from the free online

An Introduction to Programming Using Python

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now