編寫一個 Python 程序,通過使用 If 語句、嵌套 If 語句和 Elif 語句來檢查閏年與否。在我們進入 Python 閏年程序之前,讓我們看看 Python 閏年背後的邏輯和定義。
正常年份包含 365 天,但閏年包含 366 天。從邏輯上講,除了百年之外,所有能被 4 整除的年份都叫做閏年。
世紀年意味著它們以 00 結尾,如 1200、1300、2400、2500 等。(顯然它們可以被 100 整除)。對於這百年來說,我們還要進一步計算,才能查到 Python 中的閏年。
- 如果世紀年可以被 400 整除,那麼那一年就是 Python 中的閏年。
- 如果不能被 400 整除,那麼那一年就不是 Python 閏年。
這個針對閏年的 python 程序允許用戶輸入任何一年,並使用 If 語句檢查用戶輸入的年份是否是閏年。
# Check Leap Year using If Statement
year = int(input("Please Enter the Year Number you wish: "))
if (( year%400 == 0)or (( year%4 == 0 ) and ( year%100 != 0))):
print("%d is a Leap Year" %year)
else:
print("%d is Not the Leap Year" %year)
讓我們檢查閏年和正常年份(不是閏年)
Please Enter the Year Number you wish: 1200
1200 is a Leap Year
>>>
Please Enter the Year Number you wish: 1300
1300 is Not the Leap Year
在這個 Python 閏年程序中,由於我們必須在一個 If 語句中檢查多個條件,所以我們使用了邏輯 AND 和邏輯 OR 運算符。讓我們劃分條件,以便更好地理解它
1:(年份%400 == 0)或
2:(年份%4 == 0)和
3:(年份%100 == 0))
在這個 python 程序中,第一個條件(年份%400 == 0)將檢查(年份%400)提醒是否正好等於 0。根據演算法,任何能被 400 整除的數字在 Python 中都是閏年。
運籌學
第二個如果 statemen 用邏輯 AND 運算符保存 2 個語句,那麼它們都必須為真。
第一個條件(年份%4 == 0)將檢查(年份%4)的提醒是否正好等於 0。如果條件為假,則它將退出該條件,因為檢查其他條件沒有意義。絕對不是閏年。和
第二個條件將檢查(年份% 100)提醒是否不等於 0。如果它是真的,那麼給定的數字不是世紀數。
根據演算法,任何能被 4 整除但不能被 100 整除的數都是 Python 閏年。
提示:請參考邏輯運算符了解 Python 邏輯與和邏輯或的功能。
這個閏年程序允許用戶輸入任何一年。然後用 Python Elif 語句檢查用戶輸入的年份是否為閏年。
# Python Program to Check Leap Year using Elif Statement
year = int(input("Please Enter the Year Number you wish: "))
if (year%400 == 0):
print("%d is a Leap Year" %year)
elif (year%100 == 0):
print("%d is Not the Leap Year" %year)
elif (year%4 == 0):
print("%d is a Leap Year" %year)
else:
print("%d is Not the Leap Year" %year)
Please Enter the Year Number you wish: 2022
2022 is Not the Leap Year
>>>
=================== RESTART: /Users/suresh/Desktop/Python Leap Year.py ===================
Please Enter the Year Number you wish: 1300
1300 is Not the Leap Year
>>>
=================== RESTART: /Users/suresh/Desktop/Python Leap Year.py ===================
Please Enter the Year Number you wish: 1200
1200 is a Leap Year
在這個檢查閏年的 python 程序中,首先,用戶將輸入任何一年來檢查該年是否是閏年。
- 第一個如果條件將檢查(年份% 400)提醒是否正好等於 0。根據演算法,任何能被 400 整除的數都是閏年。如果這個條件失敗,那麼它將進入下一個條件。
- 第二如果條件將檢查(年% 100)提醒是否完全等於 0。根據演算法,任何不能被 400 整除但能被 100 整除的數字都不是閏年(世紀年)。我們在第一個 If 語句中檢查了(第 400 年)。因為它失敗了,所以它變成了第二種情況。如果第一個和第二個條件都失敗,那麼它將轉到第三個條件。
- Python 閏年程序中的第三個條件會檢查 year mod 4 是否等於 0。如果這個條件為真,那麼給定的年份就是閏年,因為我們已經在前面的條件中檢查了百年。如果所有語句都失敗了,那麼它將在最後變成 Else 語句。
- 如果以上所有陳述都失敗了,那麼這不是 Python 閏年
這個 Python 閏年程序允許用戶輸入任何一年。然後使用嵌套 If 語句檢查用戶輸入的年份是否為閏年。
# Check Leap Year using Nested If Statement
year = int(input("Please Enter the Year Number you wish: "))
if(year%4 == 0):
if(year%100 == 0):
if(year%400 == 0):
print("%d is a Leap Year" %year)
else:
print("%d is Not the Leap Year" %year)
else:
print("%d is a Leap Year" %year)
else:
print("%d is Not the Leap Year" %year)
這個 python 閏年程序允許用戶輸入任何一年來檢查該年是否是閏年。第一個如果條件將檢查(第%4 年)的提醒是否正好等於 0。
- 如果條件為假,給定的數字肯定不是閏年。
- 如果條件為真,那麼我們要進一步檢查百年。所以編譯器將進入嵌套的 If 條件。
第二,如果 Python 閏年程序中的條件將檢查(年份%100)提醒是否完全等於 0。
- 如果該條件為假,則年份不是世紀年。所以給定的數字肯定是閏年。
- 如果條件為真,那麼我們必須檢查這個數是否能被 400 整除。所以編譯器會轉到另一個嵌套的 If 條件。
此條件檢查(年份%400)的剩餘部分是否完全等於 0。
- 如果條件為假,那麼給定的數字肯定不是閏年。
- 如果條件為真,那麼給定的數字是閏年。
原創文章,作者:II8YK,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126704.html