Before session 4
Loops and iterations
Watch the videos. Next four videos (part 1, part 2, part 3 and part 4) show how Python repeats statements using looping structures.
Slides
Text-book: chapter 4 - iteration
Test
Go to Moodle and take the fourth test on the video lectures.
Exercises
This week we are going to get an insight into loops (while and for).
Example 1
In many cases, it is possible to solve an exercise using both the while and for loops. The following programs calculate the sum of the figures from 1 to 9. The first program contains the for-loop and the second one uses the while-loop.
print("Finding sum with for loop") sum1 = 0 for i in [1,2,3,4,5,6,7,8,9]: sum1 = sum1 + i print("Sum so far", sum1) print("Sum of numbers 1-9 is", sum1) print("Finding sum with while loop") sum2 = 0 a = 1 while a < 10: sum2 = sum2 + a print("Sum so far", sum2) a += 1 print("Sum of numbers 1-9 is", sum2)
Example 2
The following program is a modified example from last week - The maximum program. The present program contains a loop which repeatedly asks the user for numbers and outputs the greatest of these numbers. The program continues asking until the user enters “done” instead of the first number. If the user enters something else but integers, the program processes the error using try and except, prints out a message and skips to the next number.
def maximum(a, b): if a < b: return b else: return a while True: line = input("Enter the first number or word 'done': ") if line == 'done': break line2 = input("Enter the second number: ") try: x = int(line) y = int(line2) m = maximum(x, y) print("The maximum of numbers", x, "and", y, "is", m) except: print("Please enter a number")
Exercise 1. Part of the multiplication table
Write a program which prompts the user for one integer. The program then multiplies the entered number by the numbers from 1 to 9. The result of the calculations has to be printed out in the way shown below.
Run the program repeatedly to test it on different input values.
Here are some examples of the program output.
Please enter a number: 3 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27
Please enter a number: 9 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81
Exercise 2. Number of days in months
Upgrade the number of days in months program : add a loop which repeatedly asks the user for the numbers of months and outputs the number of days in that month until the user enters “done”. The program does not have to perform the check for a leap year (lets say that the number of days in February is always 28). If the user enters something else but a number, the program processes the error using try and except, prints out an error message and asks for another number.
Here is an example of the program output.
Enter the number of a month or word 'done': 3 That month has 31 days in it Enter the number of a month or word 'done': 4 That month has 30 days in it Enter the number of a month or word 'done': blabla Please enter a number Enter the number of a month or word 'done': -1 The number of a month must be in the range 1-12 Enter the number of a month or word 'done': 100 The number of a month must be in the range 1-12 Enter the number of a month or word 'done': 10 That month has 31 days in it Enter the number of a month or word 'done': done
Go to Moodle and put your solutions into Exercises 4.