Skip to main content

✍️ Practice Conditions

NoteLearning Objectives Checklist

1. Temperature

Complete the following sample of code given the statement below and print a message when the condition is met:

If the temperature is below zero, display it’s freezing.

<p>What logical operator is needed to express "less than"?</p>

2. Scholarship

Complete the following sample of code given the statement below and print a message when the condition is met:

Display “you are eligible for a scholarship” if the age of a person is between 18 and 25 (inclusive)

<p>What logical operators are needed to express "less than or equal to" and "greater than or equal to"?</p>

3. Password

Complete the following sample of code given the statement below and print a message when the condition is met:

Display “Successful login” if the password is “L0ngP@33w0rd34512”

<p>What logical operator is needed to express "equal to"?</p>

Note: This is not really how passwords are used in programs – writing your password down as a variable in a program is a really bad idea, because everyone who can read your code can read your password! Most programs store the hashed version of the password – click this link if you want to learn more.

4. Bus Arrival

Complete the following sample of code given the statement below and print a message when the condition is met:

I will go to school if the bus has arrived

<p>How can you use a boolean variable in an if statement?</p>

5. Percentage of increase

Complete the following function given the statement below and print a message when the condition is met:

Ensure that the percentage of increase is between -100.0 and +100.0. If it’s the case calculate the increase amount

<p>Here you have to test a condition and calculate a percentage, but only if the condition is true.</p>

6. From code to english

Translate (to English) the meaning of the following lines of code.

r_score = 35
if r_score >= 30:
    print("You are eligible for the scholarship.")
<p>Your answer should be in the form: If (some condition is met), then (some event takes place).</p>

If the r-score is above 30, a message saying “You are eligible for a scholarship” is displayed.

7. From code to english 2

Translate the following lines of code into human language:

temperature = 12.0

if 0 <= temperature < 16:
    print("It is chilly outside.")
<p>Take care to be precise about whether something is strictly less/greater than, or less/greater than or equal to (it's different!)</p>

If the temperature is greater than or equal to 0 and if the temperature is less than 16, a message saying “It is chilly outside.” is displayed.

8. Predict the output

Write down the messages that are printed at the end of this program.

<p>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.</p>

The temperature is 9.5
The humidity is 0.5
Is it raining outside? False