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

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.pyPlease provide the mark of student 1: 10Student 1 grade: APlease provide the mark of student 2: 5Student 2 grade: BPlease provide the mark of student 3: 3Student 3 grade: FPlease provide the mark of student 4: 8Student 4 grade: APlease provide the mark of student 5: 7Student 5 grade: BPlease provide the mark of student 6: 6Student 6 grade: BPlease provide the mark of student 7: 7Student 7 grade: BPlease provide the mark of student 8: 8Student 8 grade: APlease provide the mark of student 9: 9Student 9 grade: APlease provide the mark of student 10: 9Student 10 grade: AThe 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.pyProvide your guess: 3Wrong input. Try again.Provide your guess: 10Wrong input. Try again.Provide your guess: 9Wrong input. Try again.Provide your guess: 100Wrong input. Try again.Provide your guess: 7Congrats! 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!