My Weather Forecast

rainbow

Learning Objectives


In this challenge we are going to use selection blocks to display a weather forecast report.

Selection blocks uses if, elif and else statements to decide which block of code to run.

if statements use comparison operators to compare values and make a decision based on the comparison.

The main comparison operators are:

Comparison Operator Meaning Example
== is equal to 4==4
!= is different from 4!=7
< is lower than 3<5
> is greater than 9>2
<= is lower or equal to 3<=5
5<=5
>= is greater or equal to 9>=2
5>=5

When using comparison operators the result of a comparison is either true or false. For instance:

  • 3 < 5 returns true,
  • 3 > 5 returns false.

It is also possible to combine comparisons together using boolean operators such as and and or. This enables to check if more than one comparison are true (and operator) or whether at least one of the comparisons is true (or operator). e.g.

  • (3 < 5) and (3 < 7) returns true,
  • (3 == 5) or (3 < 9) returns true,
  • (3 == 5) and (3 < 9) returns false.

Check the code


Check the following code and notice how it is using:

  • Selection structures (using if statements),
  • Comparison operators (<, <=, >, >=, !=, ==)
  • Boolean logic (AND, OR) when using comparison operators within if statements

Task #1: Tweak the code


Your challenge consists of adding more to this weather report.
The same way this report randomly finds out if today will be sunny or rainy, add some code to find out whether it will be icy and whether it will be snowy.

If the weather is icy then display a message saying: “Be careful not to fall over on black ice.”

If the weather is either icy or snowy then display a message saying: “Put your gloves on, it’s going to be cold today.”

Task #2:


Add a criteria to find out whether it will be windy or not.

If the weather is both sunny and windy write a message saying: “It’s a good time to go sailing!”

If the weather is sunny but not windy write a message saying: “A perfect day to go to the beach!”

Task #3:


Add your own criterias and messages to complete this weather forecast report further.

Share Button
Posted in Computer Science, Python - Beginner, Python Challenges Tagged with: ,

Our Latest Book

Follow this blog