115103140 判斷敘述式
1.notebook
2.youtube
3.學習卡測驗
4.寫1個程式需附加說明 Create a README For Github
1. Leap Year Checker
📌 Description
This Python program asks the user to enter a year and checks whether the year is a leap year.
A leap year has 366 days, including February 29. A normal year has 365 days.
💻 Python Code
year = int(input('Enter year: '))
if (year % 4 == 0) and (not (year % 100 == 0)) or (year % 400 == 0):
print('Year', year, 'is a Leap Year')
else:
print('Year', year, 'is not a Leap Year')
🧮 Leap Year Rules
A year is a leap year if:
It is divisible by 4 but not divisible by 100; or
It is divisible by 400.
▶️ Example Output
Enter year: 1944
Year 1944 is a Leap Year
📝 Explanation
input()asks the user to enter a year.int()converts the entered value into an integer.%is the modulus operator. It returns the remainder after division.year % 4 == 0checks whether the year is divisible by 4.not (year % 100 == 0)checks whether the year is not divisible by 100.year % 400 == 0checks whether the year is divisible by 400.ifandelsedisplay the appropriate result.
🛠️ Requirements
Python 3.x
🚀 How to Run
Save the code as
leap_year.py.Open a terminal or command prompt.
Run the following command:
python leap_year.py
📚 Learning Objectives
Using
input()to receive user input.Converting input to an integer using
int().Using the modulus operator
%.Applying logical operators such as
and,or, andnot.Using conditional statements to make decisions.
📄 License
This project is for educational purposes.
中文程式說明
這個程式用來判斷使用者輸入的年份是否為閏年(Leap Year)。
閏年的判斷規則
年份可以被 4 整除,但不能被 100 整除,就是閏年。
年份可以被 400 整除,也是閏年。
其他情況則不是閏年。
程式執行範例
輸入 1944:
1944 % 4 == 0:成立。1944 % 100 == 0:不成立。因此,1944 是閏年。
輸出結果:
Enter year: 1944
Year 1944 is a Leap Year









留言
張貼留言