Before session 2
Conditional execution
Work through topics from the reading materials or watch the videos.
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.
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
The deadline for homework is Monday 23:59 Estonian time.