Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Temperature Range Check

Classify a temperature as cold, warm, or hot using Python. Beginners examine ordered thresholds, decimal input, and exact boundary values at twenty and thirty.

BeginnerPhase 1 - Level 1: Simple ConditionsExample 8 of 10
temperature-range-check.py
1# Take temperature as input
2temp = float(input("Enter temperature: "))
3
4# Classify temperature
5if temp < 20:
6 print("Cold")
7elif temp < 30:
8 print("Warm")
9else:
10 print("Hot")

Output

Enter temperature: 15
Cold

Enter temperature: 25
Warm

Enter temperature: 35
Hot

In this one

  • Build ordered intervals
  • Include threshold boundaries
  • Avoid redundant comparisons

How it works

  1. 1

    Read temp as a float.

  2. 2

    Test below twenty, then below thirty.

  3. 3

    Print Cold, Warm, or the remaining Hot case.

Edge cases

What happens at 20?

What happens at 20?

The first test fails and the second succeeds, so it is Warm.

What happens at 30?

What happens at 30?

Both tests fail, so it is Hot.

What's going on

Temperature labels here come from classroom thresholds, not a universal weather standard. float accepts decimal readings, and the first test checks for values below twenty.

Once the first test fails, Python already knows the value is at least twenty. The next test therefore covers twenty up to, but not including, thirty. else handles thirty and above. For 25, only Warm is printed.

What happens at 20? The first test fails and the second succeeds, so it is Warm.

What happens at 30? Both tests fail, so it is Hot.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which values reach the second branch?

Pick an answer to check it.

Question 2

What happens at 20?

Pick an answer to check it.

Questions people ask

What happens at 20?

The first test fails and the second succeeds, so it is Warm.

What happens at 30?

Both tests fail, so it is Hot.

Which values reach the second branch?

20 <= temp < 30.