Skip to main content

✍️ Complex Conditions

NoteLearning Objectives Checklist

✍️ Practice

1. Password 2.0

Complete the following sample of code given the statement below and print a message when the condition is met and another message if it’s not met:

Display “Successful login” if the password is “L0ngP@33w0rd34512”, and “Unsuccessful login” if the password is incorrect

Use if and else!

2. Opening Hours

Complete the following sample of code given the statement below and print a message when the condition is met and another message if it’s not met:

Display “The store is open” if the current hour is between 8 and 17, otherwise display the store is closed:

Use if and else!

3. Ascending order

Write a function which takes two numbers as input parameters and prints the two numbers in ascending order (no return):

You should write a conditional statement that can print the two numbers in a different order, depending on which number is bigger of the two.

4. Code to English

Translate the following python code to English:

def print_salary(salary, paygrade, level):
    if paygrade == 7:
        if 0 <= level <= 8:
            salary *= 1.05
        else:
            salary *= 1.04
    else:
        salary *= 1.06
    print("Your salary is", salary)

Your answer should be in the form: If (condition) then (some event occurs), otherwise (other event occurs), etc.

If the paygrade is 7, the salary is determined by the level. If the level is between 0 and 8, then the salary gets a 5% bonus, otherwise the salary gets a 4% bonus. If the paygrade is not 7, the salary gets a 6% bonus.

5. Temperature ranges

Write a program which asks the user for the temperature. The program prints a different message for each of the following condition:

If the temperature is:

  • above 25: it’s hot outside
  • below 25 but above 15: it’s pretty warm outside
  • below 15 but above 0: it’s pretty chill outside.
  • below zero: it’s freezing outside

Make sure you start from the top and update the values of the variables as each assignment/arithmetic operator is applied, in order to get the correct answer at the end.

🧩 Problem Solving

1. Car oil

Write a function which displays “Change your car oil” if a car’s mileage either exceeds 5000 km or its been more than 3 months since the last oil change. Additionally, there must at least 80.00$ of savings.

You can chain multiple conditions in one if statement using and/or. You can use brackets to control the order of logical operations in a sequence of operations.

2. Voting eligibility

Write a function which displays “You are eligible to vote.” if the user is a citizen and if their age at least 18. Otherwise display “You can’t vote.”

You can use boolean variables directly in an if statement, and can combine them with logical operations using and/or.

3. Camping or no?

Write a function which displays “You can go camping this weekend” if the camping site is available, if it’s not raining and they have access to a car. The function must return a boolean if all conditions are met.

Use the return statement to control what a function returns.

4. Ascending order

Write a functions which takes three numbers as input parameters and prints them in ascending order. No return:

Make sure you start from the top and update the values of the variables as each assignment/arithmetic operator is applied, in order to get the correct answer at the end.