Week 3: Conditionals

Week 3: Closure#

Syllabus#

  • Conditional execution with if statements, the keyword if followed by a Boolean expression

  • Controlling scope with indented block

  • Alternative execution with if-else statements, the keyword else

  • Chained conditionals with if-elif-else statements, the keyword elif

  • Nested conditionals

  • The concept of debugging

  • Debugging with print statement: using print() to trace and understand code execution

  • Identifying errors in code that runs but does not produce the expected output

  • Problem-solving by constructing the correct conditions

Checkpoint 3.1: Astronomical Season#

If we ignore small variations, astronomical seasons start on the following dates: the 20th of March (the first day of astronomical spring), the 21st of June (the first day of summer), the 23rd of September (the first day of autumn), and the 21st of December (the first day of winter).

Write a script that defines two integer variables: day and month. The script should then compute a string variable season containing the astronomical season for the date represented by day and month. The script should then print a message indicating the season for the given date.

Checkpoint 3.2: Flowering Period#

The flowering period of a plant in a tropical greenhouse is stored as two integer values:

  • start: the month when flowering starts

  • end: the month when flowering ends

For example, start = 11 and end = 2 means that flowering starts in November and ends in February. If both values are 7, the plant flowers only during July.

The special case of the plant not flowering at all is represented by both values being 0.

Write a Python script that defines three integer variables: start, end, and current. Assign values to start and end to represent a plant’s flowering season, and assign a value to current to represent a month of the year (1-12).

Then compute a boolean variable is_flowering that is True if the plant is flowering in the month represented by current, and False otherwise. The script should then print a message indicating whether the plant is flowering that month.