掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
創(chuàng)新互聯(lián)Python教程:

創(chuàng)新互聯(lián)成立于2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元余干做網(wǎng)站,已為上家服務(wù),為余干各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
編寫一個(gè) Python 程序,通過使用 If 語(yǔ)句、嵌套 If 語(yǔ)句和 Elif 語(yǔ)句來檢查閏年與否。在我們進(jìn)入 Python 閏年程序之前,讓我們看看 Python 閏年背后的邏輯和定義。
正常年份包含 365 天,但閏年包含 366 天。從邏輯上講,除了百年之外,所有能被 4 整除的年份都叫做閏年。
世紀(jì)年意味著它們以 00 結(jié)尾,如 1200、1300、2400、2500 等。(顯然它們可以被 100 整除)。對(duì)于這百年來說,我們還要進(jìn)一步計(jì)算,才能查到 Python 中的閏年。
這個(gè)針對(duì)閏年的 python 程序允許用戶輸入任何一年,并使用 If 語(yǔ)句檢查用戶輸入的年份是否是閏年。
# 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
在這個(gè) Python 閏年程序中,由于我們必須在一個(gè) If 語(yǔ)句中檢查多個(gè)條件,所以我們使用了邏輯 AND 和邏輯 OR 運(yùn)算符。讓我們劃分條件,以便更好地理解它
1:(年份%400 == 0)或
2:(年份%4 == 0)和
3:(年份%100 == 0))
在這個(gè) python 程序中,第一個(gè)條件(年份%400 == 0)將檢查(年份%400)提醒是否正好等于 0。根據(jù)算法,任何能被 400 整除的數(shù)字在 Python 中都是閏年。
運(yùn)籌學(xué)
第二個(gè)如果 statemen 用邏輯 AND 運(yùn)算符保存 2 個(gè)語(yǔ)句,那么它們都必須為真。
第一個(gè)條件(年份%4 == 0)將檢查(年份%4)的提醒是否正好等于 0。如果條件為假,則它將退出該條件,因?yàn)闄z查其他條件沒有意義。絕對(duì)不是閏年。和
第二個(gè)條件將檢查(年份% 100)提醒是否不等于 0。如果它是真的,那么給定的數(shù)字不是世紀(jì)數(shù)。
根據(jù)算法,任何能被 4 整除但不能被 100 整除的數(shù)都是 Python 閏年。
提示:請(qǐng)參考邏輯運(yùn)算符了解 Python 邏輯與和邏輯或的功能。
這個(gè)閏年程序允許用戶輸入任何一年。然后用 Python Elif 語(yǔ)句檢查用戶輸入的年份是否為閏年。
# 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
在這個(gè)檢查閏年的 python 程序中,首先,用戶將輸入任何一年來檢查該年是否是閏年。
這個(gè) Python 閏年程序允許用戶輸入任何一年。然后使用嵌套 If 語(yǔ)句檢查用戶輸入的年份是否為閏年。
# 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)
這個(gè) python 閏年程序允許用戶輸入任何一年來檢查該年是否是閏年。第一個(gè)如果條件將檢查(第%4 年)的提醒是否正好等于 0。
第二,如果 Python 閏年程序中的條件將檢查(年份%100)提醒是否完全等于 0。
此條件檢查(年份%400)的剩余部分是否完全等于 0。

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流