Lab 2: Conditionals and Reading Code

10 September 2021

Today's lab

The purpose of this lab is to give you practice:

  • using Boolean expressions
  • reading and writing if expressions
  • writing functions that use if expressions
  • writing examples/tests for the functions you write

Exercise 1: Greetings

Part 1

Write an if expression that produces a greeting based on the current time. For this exercise, the constant TIME can be assumed to contain the current time, expressed as military time, e.g., 9:00 a.m. is the number 0900 and 6:00 p.m. is 1800.

If the time is 5 a.m. or later and before 12 noon, the greeting should be "Good morning". If it's noon or later and before 6 p.m., it should be "Good afternoon". Otherwise, it should be "Good night".

Note: if expressions don't need to occur within functions. You only need a function if the computation depends on an input that's provided when the function is called. For example, this is valid code:

number = 111

if number == 111:

Part 2

Your friend came up with an implementation, but they're having some issues. Consider their code:

TIME = 0900

if TIME >= 0500:
  "Good morning"
else if (TIME >= 1200) and (TIME <= 1800):
  "Good afternoon"
  "Good night"

Write down an example where the code does not return what you think it should. Also write down your thoughts on how the order of if expression clauses can affect the behavior of code. To include this in your program, you can use a block comment.

  This is a block comment.
  It can be multiple lines long.

Exercise 2: Weather Alerts

Part 1: Saffir–Simpson

Consider making a simple weather app that alerts users of severe weather based on wind speed using the Saffir–Simpson scale:

CategoryWind Speed (mph)
5≥ 157
Tropical storm39–73
Tropical depression≤ 38

Write a function saffir-simpson(wind-speed :: Number) -> String, that takes in the average recorded recent wind speed (in miles per hour) and returns the category to which that wind speed belongs. For this function you can assume all inputs are integers (no decimal numbers).

››› saffir-simpson(65)
"Tropical storm"
››› saffir-simpson(111)
"Category 3"
››› saffir-simpson(0)
"Tropical depression"

Write tests for your function that address each category.

Part 2: Vassar weather

For your weather app, you wish to get a severe weather notification if the wind speed is classified as a hurricane (Category 1–5) and if the classification is for Vassar.

Write another function, severe-alert(wind-speed :: Number, latitude :: Number, longitude :: Number) -> Boolean, that will take the current wind speed and the location of the wind (latitude and longitude), and returns true if the storm is a categorized hurricane (1–5) and over Vassar, and false otherwise. The weather alert would be over Vassar if the latitude is between 41.656 and 41.693 and the longitude is between −73.908 and −73.880.

Here's are some tests you can try:

# Hurricane at Main Building
severe-alert(190, 41.686804, -73.895664) is true
# Gentler weather at Main Building
severe-alert(10, 41.686804, -73.895664) is false
# Hurricane at the Griffith Observatory, Los Angeles
severe-alert(190, 34.119108, -118.300388) is false

What if you wanted to only get an alert if the storm is the worst type (Category 5) and over Vassar? Call this function severe-alert2(wind-speed :: Number, latitude :: Number, longitude :: Number) -> Boolean. It works the same way as the previous function, but only returns true if the wind speed was "Category 5". How would this code be different from the previous version you wrote? Can you think of a way you can use the saffir-simpson function you have already written to figure out the category of the hurricane, without having to duplicate some of the code you wrote in saffir-simpson?

Submitting the Lab

  • When you've complete the exercises, show your code to me or one of our coaches.

  • Upload your lab02.arr file to the Lab 2 assignment on Gradescope.


This lab includes material adapted from Kathi Fisler and colleagues at Brown University.