Before session 4
1. Loops and iterations
Watch the lecture videos:
2. Test
Go to Moodle and take the fourth test on video lectures.
3. Examples
This week we are going to get an insight into loops (while and for), which allow execute statements in program many times.
Example 1
In many cases, it is possible to solve an exercise using either a while or a for loop. The following programs calculate the sum of the numbers from 1 to 9. The first program uses for-loop and the second one uses while-loop.
print("Finding the 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 the 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 the last week's Example 3, see The maximum program. This 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 anything 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")
4. Solve and submit
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 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
Update last week's 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 anything else but a number, the program processes the error using try and except, prints out an error message and skips to the next number.
Here is an example of program output.
Enter number of month or word 'done': 3 The number of days in this month is 31 Enter number of month or word 'done': 4 The number of days in this month is 30 Enter number of month or word 'done': blabla Please enter a number Enter number of month or word 'done': -1 The number of month must be in the range 1-12 Enter number of month or word 'done': 100 The number of month must be in the range 1-12 Enter number of month or word 'done': 10 The number of days in this month is 31 Enter number of month or word 'done': done
Go to Moodle and submit your solutions under Homework 4.