Part 2

Combining conditions

Logical operators

You can combine conditions with the logical operators and and or. The operator and specifies that all the given conditions must be true at the same time. The operator or specifies that at least one of the given conditions must be true.

For example, the condition number >= 5 and number <= 8 determines that number must simultaneously be at least 5 and at most 8. That is, it must be between 5 and 8.

number = int(input("Please type in a number: "))
if number >= 5 and number <= 8:
    print("The number is between 5 and 8")

Meanwhile, the condition number < 5 or number > 8 determines that number must be either less than 5 or greater than 8. That is, it must not be within the range of 5 to 8.

number = int(input("Please type in a number: "))
if number < 5 or number > 8:
    print("The number is not within the range of 5 to 8")

The following truth table contains the behaviour of these operators in different situations:

aba and ba or b
FalseFalseFalseFalse
TrueFalseFalseTrue
FalseTrueFalseTrue
TrueTrueTrueTrue

Sometimes it is necessary to know if something is not true. The operator not negates a condition:

anot a
TrueFalse
FalseTrue

The above example with the range of 5 to 8 excluded could also be programmed like this:

number = int(input("Please type in a number: "))
if not (number >= 5 and number <= 8):
    print("The number is not within the range of 5 to 8")

Especially in programming, logical operators are often called Boolean operators.

Combining and chaining conditions

The following program asks the user to type in four numbers. It then works out which of the four is the greatest, with the help of some conditions:

n1 = int(input("Number 1: "))
n2 = int(input("Number 2: "))
n3 = int(input("Number 3: "))
n4 = int(input("Number 4: "))

if n1 > n2 and n1 > n3 and n1 > n4:
    greatest = n1
elif n2 > n3 and n2 > n4:
    greatest = n2
elif n3 > n4:
    greatest = n3
else:
    greatest = n4

print(f" {greatest} is the greatest of the numbers.")
Sample output

Number 1: 2 Number 2: 4 Number 3: 1 Number 4: 1 4 is the greatest of the numbers.

In the above example the first condition n1 > n2 and n1 > n3 and n1 > n4 is true only if all three conditions within are true.

Programming exercise:

Age check

Points:
Loading...

/

Loading...

Please write a program which asks for the user's age. If the age is not plausible, that is, it is under 5 or something that can't be an actual human age, the program should print out a comment.

Have a look at the examples of expected behaviour below to figure out which comment is applicable in each case.

Sample output

What is your age? 13 Ok, you're 13 years old

Sample output

What is your age? 2 I suspect you can't write quite yet...

Sample output

What is your age? -4 That must be a mistake

Log in to complete the exercise.

Programming exercise:

Nephews

Points:
Loading...

/

Loading...

Please write a program which asks for the user's name. If the name is Huey, Dewey or Louie, the program should recognise the user as one of Donald Duck's nephews.

In a similar fashion, if the name is Morty or Ferdie, the program should recognise the user as one of Mickey Mouse's nephews.

Some examples:

Sample output

Please type in your name: Morty I think you might be one of Mickey Mouse's nephews.

Sample output

Please type in your name: Huey I think you might be one of Donald Duck's nephews.

Sample output

Please type in your name: Ken You're not a nephew of any character I know of.

Log in to complete the exercise.

Programming exercise:

Grades and points

Points:
Loading...

/

Loading...

The table below outlines the grade boundaries on a certain university course. Please write a program which asks for the amount of points received and then prints out the grade attained according to the table.

pointsgrade
< 0impossible!
0-49fail
50-591
60-692
70-793
80-894
90-1005
> 100impossible!

Some examples:

Sample output

How many points [0-100]: 37 Grade: fail

Sample output

How many points [0-100]: 76 Grade: 3

Sample output

How many points [0-100]: -3 Grade: impossible!

Log in to complete the exercise.

Programming exercise:

FizzBuzz

Points:
Loading...

/

Loading...

Please write a program which asks the user for an integer number. If the number is divisible by three, the program should print out Fizz. If the number is divisible by five, the program should print out Buzz. If the number is divisible by both three and five, the program should print out FizzBuzz.

Some examples of expected behaviour:

Sample output

Number: 9 Fizz

Sample output

Number: 7

Sample output

Number: 20 Buzz

Sample output

Number: 45 FizzBuzz

Log in to complete the exercise.

Nested conditionals

Conditional statements can also be nested within other conditional statements. For example, the following program checks whether a number is above zero, and then whether it is odd or even:

number = int(input("Please type in a number: "))

if number > 0:
    if number % 2 == 0:
        print("The number is even")
    else:
        print("The number is odd")
else:
    print("The number is negative or zero")

Some examples of how this program behaves:

Sample output

Please type in a number: 3 The number is odd

Please type in a number: 18 The number is even

Please type in a number: -4 The number is negative or zero

With nested conditional statements it is crucial to get the indentations right. Indentations determine which branches are linked together. For example, an if branch and an else branch with the same amount of whitespace are determined to be branches of the same conditional statement.

The same result can often be achieved using either nested conditional statements or conditions combined with logical operators. The example below is functionally no different from the example above, in the sense that it will print out the exactly same things with the same inputs:

number = int(input("Please type in a number: "))

if number > 0 and number % 2 == 0:
    print("The number is even")
elif number > 0 and number % 2 != 0:
    print("The number is odd")
else:
    print("The number is negative or zero")

Neither approach is intrinsically better than the other, but in different situations one or the other may seem more logical. In this particular example most people tend to find the first version with nesting to be more intuitive.

Programming exercise:

Leap year

Points:
Loading...

/

Loading...

Generally, any year that is divisible by four is a leap year. However, if the year is additionally divisible by 100, it is a leap year only if it also divisible by 400.

Please write a program which asks the user for a year, and then prints out whether that year is a leap year or not.

Some examples:

Sample output

Please type in a year: 2011 That year is not a leap year.

Sample output

Please type in a year: 2020 That year is a leap year.

Sample output

Please type in a year: 1800 That year is not a leap year.

Log in to complete the exercise.

Programming exercise:

Alphabetically in the middle

Points:
Loading...

/

Loading...

Please write a program which asks the user for three letters. The program should then print out whichever of the three letters would be in the middle if the letters were in alphabetical order.

You may assume the letters will be either all uppercase, or all lowercase.

Some examples of expected behaviour:

Sample output

1st letter: x 2nd letter: c 3rd letter: p The letter in the middle is p

Sample output

1st letter: C 2nd letter: B 3rd letter: A The letter in the middle is B

Log in to complete the exercise.

Programming exercise:

Gift tax calculator

Points:
Loading...

/

Loading...

Some say paying taxes makes Finns happy, so let's see if the secret of happiness lies in one of the taxes set out in Finnish tax code.

According to the Finnish Tax Administration, a gift is a transfer of property to another person against no compensation or payment. If the total value of the gifts you receive from the same donor in the course of 3 years is €5,000 or more, you must pay gift tax.

When the gift is received from a close relative or a family member, the amount of tax to be paid is determined by the following table, which is also available on this website:

Value of giftTax at the lower limitTax rate for the exceeding part (%)
5 000 — 25 0001008
25 000 — 55 0001 70010
55 000 — 200 0004 70012
200 000 — 1 000 00022 10015
1 000 000 —142 10017

So, for a gift of 6 000 euros the recipient pays a tax of 180 euros (100 + (6 000 - 5 000) * 0.08). Similarly, for a gift of 75 000 euros the recipient pays a tax of 7 100 euros (4 700 + (75 000 - 55 000) * 0.12).

Please write a program which calculates the correct amount of tax for a gift from a close relative. Have a look at the examples below to see what is expected. Notice the lack of thousands separators in the input values - you may assume there will be no spaces or other thousands separators in the numbers in the input, as we haven't yet covered dealing with these.

Sample output

Value of gift: 3500 No tax!

Sample output

Value of gift: 5000 Amount of tax: 100.0 euros

Sample output

Value of gift: 27500 Amount of tax: 1950.0 euros

Log in to complete the exercise.

You have reached the end of this section! Continue to the next section:

You can check your current points from the blue blob in the bottom-right corner of the page.