Before session 5
Loops and iterations
Watch the lecture videos:
Slides in English
Textbook in English
Quiz
Solve the quiz on loops and iterations in Moodle.
Examples
Loops (while and for) allow to execute statements in program many times.
Example 1
In many cases, it is possible to solve the problem using either a while or a for loop. The following two programs calculate the sum of all numbers from 1 to 9.
The first program uses for 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)
The second program does the same with the help of while loop.
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 contains a loop which repeatedly prompts the user for positive integers and outputs the biggest of them at the end. The program continues asking until the user enters 'done'. If the user enters something that is not an integer, the program processes the conversion error using try and except, prints out a message and skips to the next number. If the user enters an integer but it is not positive, then the program finds this out by an if statement.
def maximum(a, b): if a > b: return a else: return b m = 0 while True: line = input("Enter positive integer or 'done': ") if line == "done": break try: x = int(line) if x > 0: m = max(m, x) else: print("Your integer should be positive") except: print("Please enter an integer") if m > 0: print("Maximum is", m)
Exercises
1. Product
Write a program that prompts the user for a positive integer n and prints out the value of the expression
{$$2\cdot\frac21\cdot\frac23\cdot\frac43\cdot\frac45\cdot\ldots\cdot\frac{2n}{2n-1}\cdot\frac{2n}{2n+1}.$$}
Test your program with an increasing set of inputs. Think yourself: what value does this product approach to, as n increases?
2. Number of days
Write a function number_of_days with one integer parameter, which represents the number of month; the functions returns the number of days in that month. Numbers of months are 1, 2, ..., 12, and let's assume that it is not a leap year. If the number is some other integer, then the program should return −1.
>>number_of_days(9) 30 >>number_of_days(2) 28
Test the function with different arguments.
Write a program that repeatedly asks the user for number of month and prints out the number of days in that month, until the user enters 'done'. The program should perform all necessary input checks and call the function number_of_days only if the number of month is an integer in the range 1, ..., 12.
Enter number of month or 'done': 3 This month has 31 days Enter number of month or 'done': 4 This month has 30 days Enter number of month or 'done': fifth Please enter a valid number Enter number of month or 'done': -1 Number of month must be in the range 1-12 Enter number of month or 'done': 100 Number of month must be in the range 1-12 Enter number of month or 'done': 10 This month has 31 days Enter number of month or 'done': done
Optional modification. Implement the function number_of_days in such a way that it returns 28 or 29 for February, depending on whether the current year in the system is ordinary year or leap year.
3. Dragons, snakes, and dinosaurs
After the creation of the world three kinds of animals lived on the earth – dragons, snakes, and dinosaurs. Each dragon eats one snake as a breakfast, each snake eats one dinosaur as a lunch, and each dinosaur eats one dragon as a supper. (If there is not enough food for some animal, it stays without.)
Write a program that asks the user for the initial numbers of dragons, snakes and dinosaurs, and prints out the day of the last meal and what kinds on animals and how many of them will be left after that.
Number of dragons: 3 Number of snakes: 5 Number of dinosaurs: 9 The last meal will be on day 5 There will be 2 snakes left
Submit your solutions
Submit your solutions of exercises 1, 2, 3 in Moodle under the names home1.py, home2.py, home3.py, respectively.