Arvutiteaduse instituut
  1. Kursused
  2. 2024/25 sügis
  3. Programmeerimise alused (MTAT.03.236)
EN
Logi sisse

Programmeerimise alused 2024/25 sügis

  • 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.

  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused