Before session 2
Conditional execution
Watch the videos. Next two videos (part 1 and part 2) tell how Python executes some statements and skips others.
Slides
Text-book: chapter 3 - conditionals
Test
Go to Moodle and take the second test about the video lectures. You can get up to 2 points for the test. You are not limited in time while taking the test. You can spend as much time as you need. You can answer the questions several times. You can take the test several times. The score of the last attempt is credited.
Homework
The deadline for homework is Thursday 23:59 Estonian time. The cut off deadline is Friday 23:59 Estonian time.
This week we are into if-else and try-except statements. In real world, programs are developed by mixing these constructions (and others that we have not learned yet). For example, you can write a program that asks an input, checks if the input value is valid, and then depending on the input the program chooses what to output.
Example 1
age_as_string = input("How old are you? ") try: age = int(age_as_string) except: age = -1 if age < 1: print("This is not a valid age") elif age < 18: print("In Estonia, you are considered as a minor") elif age < 100: print("You are an adult") else: print("Wow, you need a lot of birthday candles!")
Example 2
try: x = int(input('Enter first number: ')) y = int(input('Enter second number: ')) if x == y: print('Numbers are equal') else: if x < y: print(x, 'is less than', y) else: print(x, 'is greater than', y) except: print('Please enter a number')
Homework 2.1 Estonian elections
In Estonia local and parliament elections are conducted every 4 years. The mandatory age to vote in local elections is 16 and for parliament, it’s 18. Write a program that asks the user for his/her age and tells them in which elections they can vote. Also, the program has to check the validity of the entered age: the maximum allowed age is 140, a valid age cannot be negative.
Example:
>>> %Run solution.py
Enter your age: 10
You are too young to participate in government elections in Estonia.
>>> %Run solution.py
Enter your age: 17
You can only vote at local elections.
>>> %Run solution.py
Enter your age: -4
Entered age is invalid.
>>> %Run solution.py
Enter your age: 25
You can vote in both local and parliamentary elections.
>>> %Run solution.py
Enter your age: 200
Entered age is invalid.
>>> %Run solution.py
Enter your age: x
Entered age is invalid.
If you have been struggling with this exercise for some time, maybe you can get help from troubleshooter: https://progtugi.cs.ut.ee/#/ts/620269b86449631d0c385f26/
. It tries to explain the most common problems and give hints.
Homework 2.2 Divisibility
Write a program that asks the user for a number and checks if it can be divided by 3 and/or 5. If the selected number can only be divided by 3 or 5 then print the one that it is divisible with. In case the number is divisible with both 3 and 5 the program should only print that it can be divided by both. If it’s not divisible then inform the user about it. You should also handle non-integer inputs.
Example:
>>> %Run solution.py
Enter a number: 9
Number 9 is divisible by 3
>>> %Run solution.py
Enter a number: 10
Number 10 is divisible by 5
>>> %Run solution.py
Enter a number: 15
Number 15 is divisible by 3 and 5
>>> %Run solution.py
Enter a number: 1
Number 1 cannot be divided by 3 or 5
>>> %Run solution.py
Enter a number: hello world
The inserted value was not a number
If you have been struggling with this exercise for some time, maybe you can get help from troubleshooter: https://progtugi.cs.ut.ee/#/ts/62035fa56449631d0c38629c/
. It tries to explain the most common problems and give hints.
Go to Moodle and upload your solution into Homework 2.