HW03 判斷敘述式
2.學習卡
3.影片
4. 寫1個程式需附加說明 Create a README For Github
Weather-Based Activity Recommendation
📌 Introduction
This is a simple Python program that recommends an activity based on the weather temperature and humidity.
The program uses if, else, and logical operators (and) to determine which activity is suitable for the given weather conditions.
🎯 Program Objective
The program asks the user to enter:
Temperature: Warm or Cold
Humidity: Humid or Dry
Based on the user's input, it recommends one of four activities.
💻 Source Code
temp = input('Enter the temperature: (warm/cold): ')
hum = input('Enter humidity: (humid/dry): ')
if temp == 'warm' and hum == 'dry':
print('Play Basketball')
else:
if temp == 'warm' and hum == 'humid':
print('Play Tennis')
else:
if temp == 'cold' and hum == 'dry':
print('Play Cricket')
else:
print('Swim')⚙️ Decision Rules
| Temperature | Humidity | Recommended Activity |
|---|---|---|
| Warm | Dry | Play Basketball |
| Warm | Humid | Play Tennis |
| Cold | Dry | Play Cricket |
| Cold | Humid | Swim |
▶️ Example Output
Enter the temperature: (warm/cold): cold
Enter humidity: (humid/dry): humid
Swim📖 Explanation
The
input()function receives the temperature and humidity from the user.The
ifstatement checks whether the temperature is warm and the humidity is dry.The
andoperator requires both conditions to be true.The
elsestatement handles other conditions and checks them using nestedifstatements.The program prints the recommended activity based on the input.
🧠 Python Concepts Learned
User input with
input()Conditional statements (
ifandelse)Comparison operators (
==)Logical operators (
and)Nested conditional statements
🚀 How to Run
Install Python 3.
Save the source code as
weather_activity.py.Open a terminal or command prompt.
Run the program:
python weather_activity.py📝 Notes
Enter the values using lowercase letters:
warm,cold,humid, ordry.The program uses the final
elsebranch to recommend swimming when the other three conditions are not matched.This project is intended for Python beginners to practice conditional logic.
Author: Your Name
Language: Python 3
Project Type: Beginner Python Exercise










留言
張貼留言