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

Exercises on loops

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

In these exercises, you will be able to practice writing loops in Python. We will provide you with some real-world problems that you will need to solve using loops. This will be a great opportunity for you to see how useful for and while loops are.

Exercise 1: For loops

Think back to the previous exercise of creating a program to classify students’ grades.

Recall that the classification is as follows:

  1. It will print ‘A’ if the mark is between 8..10 (inclusive).
  2. It will print ‘B’ if the mark is between 5..7 (inclusive).
  3. It will print ‘C’ if the mark is between 4..6 (inclusive).
  4. It will print ‘F’ if the mark is between 1..3 (inclusive).
  5. It will print ‘Wrong input’ if anything else is provided.

The problem you need to solve

The teacher wants you to extend this program now to allow them to read all the numbers of the class and print the mean of the class as well. So your program should print the grade for each student, but it should use a loop to do this for 10 students and also print the mean of the class at the end.

  • Assume there are 10 students in the class.
  • At the end, the program should also print the mean of the class. The mean is the total marks of the class divided by the number of students. That is, if the total of all marks from all the students is 75 and there are 10 students, the mean will be 7.5 (75 divided by 10). 

Prepare

The files you will need to complete these exercises, and the solution to them, can be found in the Supporting files and solutions .zip file. Use the files in the folder ‘lab 5’. 

Open Visual Studio Code and then open the for_loops.py file. You will see a skeleton code with multiple TODO comments to help you complete the exercise. Each TODO block you will see (they appear as Python comments with # and the keyword TODO) is a small sub-task. 

Complete the code

Have a look at all the TODO comments and complete the code required for each.
Running the code: Open a terminal (select Terminal → New Terminal from the menu bar on the top) and type python for_loops.py in the terminal pane (and hit the ENTER key) to run the program.
Example output:
❯ python for_loops.py
Please provide the mark of student 1: 10
Student 1 grade: A
Please provide the mark of student 2: 5
Student 2 grade: B
Please provide the mark of student 3: 3
Student 3 grade: F
Please provide the mark of student 4: 8
Student 4 grade: A
Please provide the mark of student 5: 7
Student 5 grade: B
Please provide the mark of student 6: 6
Student 6 grade: B
Please provide the mark of student 7: 7
Student 7 grade: B
Please provide the mark of student 8: 8
Student 8 grade: A
Please provide the mark of student 9: 9
Student 9 grade: A
Please provide the mark of student 10: 9
Student 10 grade: A
The mean of the class is 7.2

Exercise 2: While loops

You are asked to implement a text-based ‘guess the number’ game in Python. This will be an interactive game between the computer and the user. There will be a secret number and the user will need to guess that number.

The problems you need to solve

Your program will enclose a secret number of your choice. Then, the user will be asked to guess the secret number, ideally with the least number of guesses. Your program will need to keep a counter to count the number of guesses until the user hits the secret. At the end, if the user finds the secret, it should print a congrats message to the user but also the number of guesses it took for the user to find the secret. Your program should keep asking the user for a guess until the secret is found.

Prepare

Open Visual Studio Code and then open the while_loops.py file. You will see a skeleton code with multiple TODO comments to help you complete the exercise. Each TODO block you will see (they appear as Python comments with # and the keyword TODO) is a small sub-task. 

Complete the code

Have a look at all the TODO comments and complete the code required for each.
Running the code: Open a terminal, if not already opened, (select Terminal → New Terminal from the menu bar on the top) and type python while_loops.py in the terminal pane (and hit the ENTER key) to run the program.

Example output:

❯ python while_loops.py
Provide your guess: 3
Wrong input. Try again.
Provide your guess: 10
Wrong input. Try again.
Provide your guess: 9
Wrong input. Try again.
Provide your guess: 100
Wrong input. Try again.
Provide your guess: 7
Congrats! The secret number is 7. You found the secret after 5 attempts.

Solutions

Remember, 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. Some code can be more efficient than others or more readable. Read my code anyway and see if there is something I do differently, and reflect on why or see if you think it is better. As we said before, a good programmer is one that reads 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