Institute of Computer Science
  1. Courses
  2. 2018/19 spring
  3. Computer Programming (LTAT.03.001)
ET
Log in

Computer Programming 2018/19 spring

  • Home

Before session 3

Conditional execution

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

Slides in English

Textbook in English

Quiz

Go to Moodle and solve the third quiz about video lectures. You can solve the quiz several times and answer questions several times. Best attempt counts.

Examples

This week we are into if-else and try-except statements. In real world, programs are developed by mixing these construcs (and others that we'll learn in the future). 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("You are 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 the first number: '))
    y = int(input('Enter the 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')

Exercises

1. Name of the month

Write a program that asks the user to enter month number and then prints out the name of the month.

If the user enters an integer outside the range 1 to 12, then the programs should print an informative message. You can assume that the user enters only integers. Test your program with different inputs.

Here are some examples of program output.

 Enter month number: 2
 February

 Enter month number: 0
 Month number must be in the range [1-12]

 Enter month number: 13
 The year has only 12 months

2. Food energy

Write a program that converts the energy amount of a food between two units of measure commonly used on food labels: kcal and kJ (kilocalories and kilojoules). Their exact relationship is 1 kcal = 4.1868 kJ. Some food labels have this information only in one unit of measure, so conversion is needed.

The program has to ask for two inputs: numeric value and unit of measure (kcal or kJ). The program then performs the conversion and outputs the result in the other unit of measure.

If the user enters non-numeric value for the amount, then the program prints out an informative message. If the user enters a unit of measure other than kcal or kJ, then program also informs the user with a message. (Hint: use try-except.)

 Enter the amount: 250
 Enter the unit: kcal
 This amount corresponds to 1046.7 kJ

 Enter the amount: 8000
 Enter the unit: kJ
 This amount corresponds to 1910.7671730199677 kcal

 Enter the amount: a couple of hundred
 Enter the unit: kcal
 Please enter the amount as numeric value

 Enter the amount: 356
 Enter the unit: cal
 Please use only the units kcal or kJ

3. Tax-free income

The amount of tax-free income in Estonia currently depends on the annual income:

  • for annual income up to 6000 euros, tax-free income is equal to annual income
  • for annual income from 6000 euros to 14400 euros, tax-free income is 6000 euros
  • for annual income from 14400 euros to 25200 euros, tax-free income is computed according to the formula 6000 – 6000 ÷ 10800 × (annual_income – 14400)
  • for annual income over 25200 euros, tax-free income is 0 euros.

Write a program that asks for user's annual income (nonnegative floating point number) and computes and outputs the tax-free income rounded to two decimal places.

Example

 Enter your annual income: 16825
 Your tax-free income is 4652.78 euros

Hint

>>> round(16.6333, 2)
16.63

Submit your solutions

Submit your solutions of exercises 1, 2, 3 to the autograder in Moodle, under Homework for Session 3.

NB! Use home1.py, home2.py, home3.py as the names of your files.

  • 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