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.
1# Take temperature as input2temp = float(input("Enter temperature: "))34# Classify temperature5if 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
Read temp as a float.
- 2
Test below twenty, then below thirty.
- 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.
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum