Arvutiteaduse instituut
  1. Kursused
  2. 2018/19 sügis
  3. Programmeerimine (Narva Kolledž) (SVNC.00.054)
EN
Logi sisse

Programmeerimine (Narva Kolledž) 2018/19 sügis

  • Home
  • Grading
  • Links

Before session 2

1. Conditional execution

Watch the video about how Python executes some statements and skips others.:

Slides in English and in Russian

Textbook in English

2. Test

Go to Moodle and take the second test about the video lectures. The maximum points for the test is 1. The time is not limited 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.

3. Examples

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')

4. Solve and submit

Exercise 1. Admission to the Univeristy of Tartu

Write a program to solve the following problem.

The University of Tartu uses different coefficients to calculate the admission score for each curriculum. A student's application is considered for admission only if the admission score is 66 points or higher. Your task is to write a program that asks the user for the admission score. The program has to output whether the obtained score is enough to be considered for admission to the University of Tartu. Also the program has to check the validity of the entered score: the maximum points for all curricula are 100.

Some examples of the program output are shown below:

 Enter the number of points: 85
 The obtained points are enough to be considered for admission 


 Enter a number of points: 65
 The obtained points are not enough to be considered for admission 


 Enter a number of points: 115
 You cannot obtain so many points 


 Enter a number of points: -1
 You cannot obtain negative points 


Exercise 2. Number of days in months

Write a program that asks the user to enter the number of a month. The program has to output the number of days in that month. Also make sure if the user inputs anything ridiculous, the program has to give a corresponding feedback about it. Use try-except.

Test your program with different inputs and make sure the program works correctly. For example: -100, 0, 1, 2, 5, 9, 10, 12, 13, 100, blabla

Here are some examples of the program output.

 Enter the number of a month: blabla
 Please enter a number


 Enter the number of a month: 0
 The number of a month must be in the range [1-12]


 Enter the number of a month: 12
 That month has 31 days in it.


 Enter the number of a month: 4
 That month has 30 days in it.


Optional part

If you can (and it is not very difficult at all) also try to check against the leap year. In that case, the program has to ask the user about the year.

How to check for the leap years: the leap year is a year that is divisible by 4 and is not divisible by 100, or when the year is divisible by 400.
This description might be helpful when trying to solve the task:
1. If a year is not exactly divisible by 4 then it is a common year.
2. If the year is divisible by 4 and is not divisible by 100, the year is a leap year.
3. If the year is divisible by 100 and 4, then the year is a leap year only if the year is also divisible by 400. Otherwise it is not.
The other opportunity to determine whether a year is a leap year:
1. If the year is evenly divisible by 4, go to step 2. Otherwise, print out that February has 28 days (the year is a common year).
2. If the year is evenly divisible by 100, go to step 3. Otherwise, print out that February has 29 days (the year is a leap year).
3. If the year is evenly divisible by 400, print out that February has 29 days (the year is a leap year). Otherwise, print out that February has 28 days (the year is not a leap year).

Here are some examples of the program output if the entered number is 2 (February):

 Enter the number of a month: 2
 Enter a year: 2015
 That month has 28 days in it (a common year).


 Enter the number of a month: 2
 Enter a year: 2000
 That month has 29 days in it (a leap year).


 Enter the number of a month: 2
 Enter a year: 2100
 That month has 28 days in it (common year).


 Enter the number of a month: 2
 Enter a year: 2016
 That month has 29 days in it (a leap year).


Go to Moodle and upload your solutions into Homework 2.

  • 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