HW03 判斷敘述式
2.Youtube
3.測驗
4.學習卡
5.寫1個程式需附加說明 Create a README For Github
以下使用 Python 寫一個判斷閏年的程式,並附上中英文說明及適合放在 GitHub 的 README.md。
1. Python 程式:leap_year.py
year = int(input("Enter a year: "))if year % 400 == 0: print(year, "is a leap year.")elif year % 100 == 0: print(year, "is not a leap year.")elif year % 4 == 0: print(year, "is a leap year.")else: print(year, "is not a leap year.")2. 程式說明(Program Explanation)
閏年(Leap Year)的判斷規則:
年份可以被 400 整除,就是閏年。
年份可以被 100 整除,但不能被 400 整除,就不是閏年。
年份可以被 4 整除,但不能被 100 整除,就是閏年。
其他情況都不是閏年。
Python 的 % 是取餘數運算子。例如,2024 % 4 == 0 代表 2024 可以被 4 整除。
3. 執行結果(Example Output)
Example 1
Enter a year: 2024
2024 is a leap year.Example 2
Enter a year: 1900
1900 is not a leap year.Example 3
Enter a year: 2000
2000 is a leap year.4. GitHub README.md
建立一個名為 README.md 的檔案,將以下內容複製進去即可。
Leap Year Checker
📌 Introduction
This is a simple Python program that prompts the user to enter a year and checks whether it is a leap year.
這是一個簡單的 Python 程式,讓使用者輸入年份,並判斷該年份是否為閏年。
🎯 Features
Prompts the user to input a year.
Checks whether the year is a leap year.
Displays the result on the screen.
🛠️ Requirements
Python 3.x
📂 Project Structure
leap-year-checker/
├── leap_year.py
└── README.md💻 How to Run
Clone or download this repository.
Open a terminal in the project folder.
Run the following command:
python leap_year.py🧠 Leap Year Rules
A year is a leap year if:
It is divisible by 400.
Or it is divisible by 4 but not by 100.
Otherwise, it is not a leap year.
閏年判斷規則:
年份可以被 400 整除,就是閏年。
或者年份可以被 4 整除,但不能被 100 整除,也是閏年。
其他情況都不是閏年。
▶️ Example
Enter a year: 2024
2024 is a leap year.📚 What I Learned
Python input and output
Integer conversion using
int()Conditional statements (
if,elif,else)Modulo operator (
%)
📄 License
This project is available for educational purposes.
5. 如何上傳到 GitHub
登入 GitHub,建立一個新的 Repository。
Repository 名稱可設為
leap-year-checker。上傳
leap_year.py和README.md兩個檔案。- 完成後,GitHub 會自動在專案首頁顯示 README 的內容。
這樣就完成了一個包含程式碼、執行範例、中英文說明及 GitHub README 的小型 Python 專案。










留言
張貼留言