Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Estimated time

10-20 minutes

Level of difficulty

Easy/Medium

Objectives

Familiarize the student with:

  • using the if-else instruction to branch the control path;
  • building a complete program that solves simple real-life problems.

Scenario

Once upon a time there was a land - a land of milk and honey, inhabited by happy and prosperous people. The people paid taxes, of course - their happiness had limits. The most important tax, called the Personal Income Tax (PIT for short) had to be paid once a year, and was evaluated using the following rule:

  • if the citizen's income was not higher than 85,528 thalers, the tax was equal to 18% of the income minus 556 thalers and 2 cents (this was the so-called tax relief)
  • if the income was higher than this amount, the tax was equal to 14,839 thalers and 2 cents, plus 32% of the surplus over 85,528 thalers.

Your task is to write a tax calculator.

  • It should accept one floating-point value: the income.
  • Next, it should print the calculated tax, rounded to full thalers. There's a function named round() which will do the rounding for you - you'll find it in the skeleton code in the editor.

Note: this happy country never returns money to its citizens. If the calculated tax is less than zero, it only means no tax at all (the tax is equal to zero). Take this into consideration during your calculations.

Look at the code in the editor - it only reads one input value and outputs a result, so you need to complete it with some smart calculations.

Test your code using the data we've provided.

Test Data

Sample input: 10000

Expected output: The tax is: 1244.0 thalers

Sample input: 100000

Expected output: The tax is: 19470.0 thalers

Sample input: 1000

Expected output: The tax is: 0.0 thalers

Sample input: -100

Expected output: The tax is: 0.0 thalers

Key takeaways

 

1. The comparison (otherwise known as relational) operators are used to compare values. The table below illustrates how the comparison operators work, assuming that x = 0, y = 1, and z = 0:

OperatorDescriptionExample

==returns True if operands' values are equal, and False otherwise

!=returns True if operands' values are not equal, and False otherwise

x != y # True

x != z # False

>True if the left operand's value is greater than the right operand's value, and False otherwise

x > y # False

y > z # True

<True if the left operand's value is less than the right operand's value, and False otherwise

x < y # True

y < z # False

≥True if the left operand's value is greater than or equal to the right operand's value, and False otherwise

x >= y # False

x >= z # True

y >= z # True

≤True if the left operand's value is less than or equal to the right operand's value, and False otherwise

x <= y # True

x <= z # True

y <= z # False

2. When you want to execute some code only if a certain condition is met, you can use a conditional statement:

  •  a single if statement

x = 10

if x == 10: # condition
    print("x is equal to 10")  # Executed if the condition is True.

  • a series of if statements

x = 10

if x > 5: # condition one
    print("x is greater than 5")  # Executed if condition one is True.

if x < 10: # condition two
    print("x is less than 10")  # Executed if condition two is True.

if x == 10: # condition three
    print("x is equal to 10")  # Executed if condition three is True.
    
Each if statement is tested separately.

  • an if-else statement, e.g.:

x = 10

if x < 10:  # Condition
    print("x is less than 10")  # Executed if the condition is True.

else:
    print("x is greater than or equal to 10")  # Executed if the condition is False.

  • The if-elif-else statement, e.g.:

x = 10

if x > 5:  # True
    print("x > 5")

if x > 8:  # True
    print("x > 8")

if x > 10:  # False
    print("x > 10")

else:
    print("else will be executed")

 

Each if is tested separately. The body of else is executed if the last if is False.

  • The if-elif-else statement, e.g.:

x = 10

if x == 10:  # True
    print("x == 10")

if x > 15:  # False
    print("x > 15")

elif x > 10:  # False
    print("x > 10")

elif x > 5:  # True
    print("x > 5")

else:
    print("else will not be executed")

 

If the condition for if is False, the program checks the conditions of the subsequent elif blocks – the first elif block that is True is executed. If all the conditions are False, the else block will be executed.

  • Nested conditional statements, e.g.:

x = 10

if x > 5:  # True
    if x == 6:  # False
        print("nested: x == 6")
    elif x == 10:  # True
        print("nested: x == 10")
    else:
        print("nested: else")
else:
    print("else")