Arvutiteaduse instituut
  1. Kursused
  2. 2020/21 kevad
  3. Programmeerimine (LTAT.03.001)
EN
Logi sisse

Programmeerimine 2020/21 kevad

  • Home

Homework for Week 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, 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 for an input, checks if it is valid, and depending on value entered, chooses what to output.

Example 1

age_as_string = input("How old are you? ")
try:
    # Try to convert the input to an integer
    age = int(age_as_string)
except:
    # Conversion failed, let age be -1 in this case
    age = -1

if age < 0:
    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: '))
except:
    print('Please enter a number')
else:
    # If both int coversions were successful, then continue here
    if x == y:
        print('Numbers are equal')
    else:
        if x < y:
            print(x, 'is less than', y)
        else:
            print(x, 'is greater than', y)

Exercises

1. Speeding fine

When the maximum driving speed is exceeded, a fine is imposed. The size of the fine is obtained by multiplying the number of km/h's above the allowed limit by 3. The maximum fine is 190 euros.

Write a program that

  • prompts the user for the name, speed limit, and actual speed;
  • calculates the fine according to the rules above;
  • outputs the name and the size of the fine on the screen.

Example

 Enter name: Jürgen
 Enter speed limit (km/h): 60
 Enter actual speed (km/h): 80

 Jürgen, your fine for speeding is €60.

Explanation: the speed limit is exceeded by 80 − 60 = 20 km/h. Therefore, the fine is 3 · 20 = 60 euros.

Second example

 Enter name: Hendrik
 Enter speed limit (km/h): 50
 Enter actual speed (km/h): 172

 Hendrik, your fine for speeding is €190.

Explanation: the speed is exceeded by 172 − 50 = 122 km/h. The calculation gives a fine of 3 · 122 = 366 euros. Since it exceeds the maximum amount of the fine, the actual fine will be 190 euros.

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

If the user enters a non-numeric value as the annual income, then the program prints a corresponding message (use try-except). Also the program prints a message, if the user enters a negative value as the annual income.

Example

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

 Enter your annual income: large
 Please enter a number!

 Enter your annual income: -100
 Please enter a non-negative value!

Hint

>>> round(16.6333, 2)
16.63

Submit your solutions

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

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

  • 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