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.
Homework
The deadline for homework is Thursday. Nevertheless, you can start with homework already before the session.
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")
Homework 4.1 Rock paper scissors
Write a program that lets the user play rock-paper-scissors against the computer. It should let the user choose between 2 options: play or quit. In the case of playing, the user must be able to choose between rock, paper, or scissors. However, the computer is broken and always chooses paper.
Your program should display the option that was picked by the computer as well as the result of the game (player's win, loss or draw).
Optional bonus task: Fix the computer so it picks randomly between all 3 options (rock, paper, scissors). Hint: For the computer’s choice, use random.choice().
Example of modified program where the computer chooses randomly:
>>> %Run solution.py
Type S to stop or P to play rock paper scissors: P
Choose rock, paper or scissors: paper
Computer chose paper
Draw
Type S to stop or P to play rock paper scissors: P
Choose rock, paper or scissors: paper
Computer chose scissors
You lost
Type S to stop or P to play rock paper scissors: S
Game ended
If you have been struggling with this exercise for some time, then maybe you can get help from the troubleshooter which tries to explain most common problems and give hints.
Homework 4.2 Sum of squares
Write a function squareSum that takes one integer as an argument and returns the sum of all integer squares from 1 to the argument value (included).
For example, if the argument is 3, then the function should calculate as follows: {$ 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14 $}
Examples of function calls:
>>> squareSum(3)
14
>>> squareSum(10)
385
Modify the program to get the integer from user input, call the function with it and display the final result.
Examples of program output:
>>> %Run solution.py
Please enter an integer: 3
The sum of integer squares from 1 to 3 is 14
>>> %Run solution.py
Please enter an integer: 10
The sum of integer squares from 1 to 10 is 385
If you have been struggling with this exercise for some time, then maybe you can get help from the troubleshooter which tries to explain most common problems and give hints.
Go to Moodle and upload your solution into Homework 4.