Exercises on loops
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:
- It will print ‘A’ if the mark is between 8..10 (inclusive).
- It will print ‘B’ if the mark is between 5..7 (inclusive).
- It will print ‘C’ if the mark is between 4..6 (inclusive).
- It will print ‘F’ if the mark is between 1..3 (inclusive).
- 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 typepython for_loops.pyin the terminal pane (and hit the ENTER key) to run the program.
❯ 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
The problems you need to solve
Prepare
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 typepython while_loops.pyin 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!
An Introduction to Programming Using Python
An Introduction to Programming Using Python
Register to receive updates
-
Create an account to receive our newsletter, course recommendations and promotions.
Register for free







