Institute of Computer Science
  1. Courses
  2. 2024/25 fall
  3. Introduction to Programming (MTAT.03.236)
ET
Log in

Introduction to Programming 2024/25 fall

  • Home
  • Grading
  • Links

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.

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.

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment