← All coding practice

Coding Practice · Beginner · October 11, 2026 · 7 min read

Python Coding Exercise #20: What to Wear Tomorrow

Part of the series Python Practice Exercise — part 20

Before you start: Know input() and int() · Know if statements and comparison operators

The program in this exercise is simple. The skill it really tests is turning a sentence into the right comparison: should it be < or <=? > or >=? Getting these wrong by one is one of the most common mistakes in programming, so this post spends extra time on it.

The question

Please write a program which asks for tomorrow’s weather forecast and then suggests weather-appropriate clothing.

The suggestion should change if the temperature (measured in degrees Celsius) is over 20, 10 or 5 degrees, and also if there is rain on the radar.

Some examples of expected behaviour:

What is the weather forecast for tomorrow?
Temperature: 21
Will it rain (yes/no): no
Wear jeans and a T-shirt
What is the weather forecast for tomorrow?
Temperature: 11
Will it rain (yes/no): no
Wear jeans and a T-shirt
I recommend a jumper as well
What is the weather forecast for tomorrow?
Temperature: 7
Will it rain (yes/no): no
Wear jeans and a T-shirt
I recommend a jumper as well
Take a jacket with you
What is the weather forecast for tomorrow?
Temperature: 3
Will it rain (yes/no): yes
Wear jeans and a T-shirt
I recommend a jumper as well
Take a jacket with you
Make it a warm coat, actually
I think gloves are in order
Don't forget your umbrella!

My Solution

print("What is the weather forecast for tomorrow?")
temperature = int(input("Temperature: "))
rain = input("Will it rain (yes/no): ")

print("Wear jeans and a T-shirt")
if temperature <= 20:
    print("I recommend a jumper as well")
if temperature <= 10:
    print("Take a jacket with you")
if temperature <= 5:
    print("""Make it a warm coat, actually
I think gloves are in order""")

if rain == "yes":
    print("Don't forget your umbrella!")

How My Solution Works

1. The base outfit is always printed

print("Wear jeans and a T-shirt") has no if in front of it, so it runs every time, whatever the weather.

2. Each cold condition

There are three temperature limits, 20, 10 and 5, and each has its own separate if. The colder it gets, the more of them are true, so the suggestions pile up:

TemperatureJumper (<= 20)Jacket (<= 10)Coat and gloves (<= 5)
21nonono
20yesnono
11yesnono
10yesyesno
6yesyesno
5yesyesyes

This is the same pattern as in Exercise #15: separate ifs, because more than one condition can be true at the same time. An elif chain would stop at the first match and miss the lower suggestions.

3. Rain is a separate question

The umbrella has nothing to do with the temperature, so it gets its own if at the end: if rain == "yes":. Since rain is a string, it’s compared with == and a quoted word. Remember that the comparison is case-sensitive, so Yes with a capital letter would not match "yes". To ignore capital letters, see String Methods in Python.

4. A multi-line print

The coat and gloves messages are printed by a single print() using a triple-quoted string, which keeps the line break between them. You could also write two separate print() calls, as the alternative below does.

Getting the comparisons right

This is the heart of the exercise. The sentence says the clothing changes “if the temperature is over 20, 10 or 5”. But look at what the examples actually show:

  • At 21, no jumper is suggested. 21 is over 20, so warm enough.
  • At 11, a jumper is suggested but no jacket. 11 is over 10, so no jacket.

So a jumper is recommended when the temperature is not over 20, which means 20 or below. That’s temperature <= 20.

From words to operators

Here is a handy translation table. Notice how often the boundary value itself is the tricky part:

In wordsPythonIs the boundary included?
over 20 / more than 20 / above 20> 20no, 20 is not included
20 or more / at least 20>= 20yes
under 20 / less than 20 / below 20< 20no
20 or less / at most 20 / not over 20<= 20yes

A useful trick is to ask: “if the value is exactly the boundary, what should happen?” For 20 degrees, the exercise should recommend a jumper (it’s not over 20), so the condition must include 20, which means <=, not <.

Test the boundaries, not just the middle

The samples use 21, 11, 7 and 3, which are all comfortably inside their ranges. The real mistakes hide exactly on the limits: 20, 10 and 5, and the values just above them, 21, 11 and 6. Before trusting your conditions, check those:

TemperatureWhat should happen
21nothing extra (over 20)
20jumper only (not over 20)
11jumper only (over 10)
10jumper and jacket (not over 10)
6jumper and jacket (over 5)
5everything, including coat and gloves (not over 5)

If you had written temperature < 20, the table would show the mistake at once: for exactly 20, the jumper would be missing.

University Solution

print("What is the weather forecast for tomorrow?")
temperature = int(input("Temperature: "))
rain = input("Will it rain (yes/no): ")
print("Wear jeans and a T-shirt")
if temperature < 21:
    print("I recommend a jumper as well")
if temperature < 11:
    print("Take a jacket with you")
if temperature < 6:
    print("Make it a warm coat, actually")
    print("I think gloves are in order")
if rain == "yes":
    print("Don't forget your umbrella!")

The University Solution uses < 21, < 11 and < 6 where My Solution uses <= 20, <= 10 and <= 5. For whole numbers, these are exactly the same: “less than 21” and “20 or less” mean the same thing when nothing sits between 20 and 21. Both programs print identical output for every whole number from -30 to 50.

It also prints the coat and gloves lines with two separate print() calls instead of one triple-quoted string. The output is the same.

A catch: decimals

The two styles are not equivalent once decimals are involved. This exercise reads the temperature with int(), so a decimal can’t be entered. But imagine the program read a float, and the forecast said 20.5 degrees. That is over 20, so no jumper is needed:

  • temperature <= 20 is False for 20.5, which is correct: no jumper.
  • temperature < 21 is True for 20.5, which is wrong: it suggests a jumper.

The <= 20 form says exactly what the exercise says, “over 20 degrees”, and keeps working if the input ever changes. The < 21 form only works because there are no whole numbers between 20 and 21. So when a sentence gives you a limit, it’s safest to use that same number in your condition, and choose the operator that matches the words.

Checking your own conditions

Here are some habits that make comparison mistakes much easier to catch:

  • Read the condition out loud as a sentence. if temperature <= 20 becomes “if the temperature is 20 or less”. Does that match the exercise?
  • Test the boundary and its neighbours. For a limit of 20, try 19, 20 and 21.
  • Think of a number line. Draw the limits and mark which side each suggestion belongs to.
  • Remember what each symbol points at. The small end of < points at the smaller value. a < b reads “a is smaller than b”.
  • Don’t mix up = and ==. A single = assigns, and == compares.

Key takeaways

  • A line with no if in front of it runs every time. Separate ifs let several suggestions pile up.
  • Translate words carefully: “over” is >, “at least” is >=, “under” is <, and “at most” or “not over” is <=.
  • Ask what should happen exactly at the limit, then choose between < and <= (or > and >=).
  • Test the boundary values and their neighbours, not just the numbers in the middle.
  • <= 20 and < 21 agree for whole numbers but disagree for decimals. Using the number from the sentence is the safest choice.
  • Strings are compared with ==, and the comparison is case-sensitive.

✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦


Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.

Tagged: python · conditionals · comparison-operators · if-statements · strings