← Schedule

HW 1: Conditionals, types & formatting

CMSC 141: Introduction to Python

Due Sunday, June 28, 11:59pm

This week's homework is about making decisions in code and showing the results clearly. You will write code that picks an answer based on the weather: is it snowing, how cold is it, can a flight take off. To do that you will use comparisons (<=, >, and the like), the words and, or, and not, and if / elif / else to choose between options. A few of the exercises also ask you to build a sentence out of numbers and words.

Everything you need was covered in class. You do not need loops, lists, or anything we have not seen yet. Stick to variables, arithmetic, comparisons, the logical words above, if/elif/else, and f-strings (explained below).

How the file works

Open hw1.py. You will see seven blocks that each begin with the word def, like this:

def is_snowing(temp_f, precip_in):
    """ ...a description of what to do... """
    ### EXERCISE 1 -- YOUR CODE GOES HERE
    result = None
    return result

You do not need to know what def means yet; we cover that in a couple of weeks. For now, think of each block as a labeled task. Your job is to replace the line marked ### EXERCISE N -- YOUR CODE GOES HERE (and the placeholder under it) with your answer.

The names in the parentheses after the task's name are the values you are given to work with. In the block above, temp_f and precip_in are the temperature and the precipitation. You must use those names in your answer: they stand in for whatever numbers get tested, so writing temp_f <= 32 checks the actual temperature, while typing a specific number like 30 would only ever check that one case. Whatever your code should produce goes after the word return. Some blocks give you a result line to fill in; others just say where your code goes, and you write the return yourself.

Solve each block using only the values it is given. You will not need to use one block inside another on this assignment; that comes later in the course.

A note on f-strings

Two exercises ask you to build a string that mixes words and numbers. The easiest way to do that is an f-string. You put the letter f right before the opening quote, and anything inside curly braces { } is replaced by the value you name there:

name = "Alice"
bank_balance = 30
print(f"{name}: ${bank_balance}")     # prints  Alice: $30

You can also control how a number looks. Writing :.3f after a value rounds it to three digits after the decimal point, so f"{20:.3f}" produces 20.000.

The exercises

  1. is_snowing(temp_f, precip_in) — return True when it is cold enough and something is falling.
  2. describe_temperature(temp_f) — return a one-word category ("freezing", "cold", "mild", "warm", or "hot") using an if/elif/else chain.
  3. cold_weather_alert(temp_f, wind_mph) — return the right alert from a small table; the top rows depend on both the temperature and the wind.
  4. format_temperature(temp_f) — return a formatted string like "72.5F, above freezing" using an f-string.
  5. weather_report(city, temp_f, precip_in) — return a one-line report like "Chicago: 30F, snowing".
  6. clothing_advice(temp_f, is_raining) — return a recommendation, adding an umbrella note when it is raining.
  7. flight_status(...) — return one of "grounded", "delayed", "caution", or "cleared".

Read the description at the top of each block carefully. It lists the exact words to return and gives a few examples of inputs and the output you should get for them.

Testing your work

Run the tester file from the hw1 directory to check it against the built-in examples:

uv run python test_hw1.py

This prints, for each exercise, the value it expected and the value your code produced. When the two lines match, that exercise is working. Fix one exercise at a time and rerun.

What to turn in

Fill in your answers in hw1.py and answer the short questions in QUESTIONS-01.txt. Then commit and push your work so it reaches the server:

git add hw1.py QUESTIONS-01.txt
git commit -m "Complete HW 1"
git push