HW03 判斷敘述式

 1.NotebookLM

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

TemperatureHumidityRecommended Activity
WarmDryPlay Basketball
WarmHumidPlay Tennis
ColdDryPlay Cricket
ColdHumidSwim

▶️ Example Output

Enter the temperature: (warm/cold): cold
Enter humidity: (humid/dry): humid
Swim

📖 Explanation

  1. The input() function receives the temperature and humidity from the user.

  2. The if statement checks whether the temperature is warm and the humidity is dry.

  3. The and operator requires both conditions to be true.

  4. The else statement handles other conditions and checks them using nested if statements.

  5. The program prints the recommended activity based on the input.

🧠 Python Concepts Learned

  • User input with input()

  • Conditional statements (if and else)

  • Comparison operators (==)

  • Logical operators (and)

  • Nested conditional statements

🚀 How to Run

  1. Install Python 3.

  2. Save the source code as weather_activity.py.

  3. Open a terminal or command prompt.

  4. Run the program:

python weather_activity.py

📝 Notes

  • Enter the values using lowercase letters: warm, cold, humid, or dry.

  • The program uses the final else branch 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


留言

這個網誌中的熱門文章