Skip to content

script_5.py - Temperature Advisor

Code

Python
#!/usr/bin/env python3
"""Temperature advisor"""

temp = float(input("Enter temperature in Celsius: "))

if temp < 0:
    print("Freezing! Wear heavy coat.")
elif temp < 10:
    print("Cold. Wear a jacket.")
elif temp < 20:
    print("Cool. Long sleeves recommended.")
elif temp < 30:
    print("Warm. T-shirt weather.")
else:
    print("Hot! Stay hydrated.")

Explanation

Line 4: temp = float(input("Enter temperature in Celsius: ")) - input() returns string type (str) - float() converts to floating-point type allowing decimals like 15.5 or -3.2 - temp variable stores the float value - Float is used instead of int to handle fractional temperatures

Line 6: if temp < 0: - < is less-than comparison operator - Checks for below freezing temperatures - First condition in cascade

Line 8: elif temp < 10: - Only evaluated if temp >= 0 (previous condition False) - Effectively checks if temp is between 0 and 9.99 - Order matters in elif chains - they're evaluated sequentially

Line 10: elif temp < 20: - Checks range 10 to 19.99 (implicitly, due to previous conditions)

Line 12: elif temp < 30: - Checks range 20 to 29.99

Line 14: else: - Catches all temperatures >= 30 - Final fallback when all previous conditions are False