我必须编写一个程序,我输入一个数字,程序会回复该数字是否为闰年。我是python的新手所以我不确定从哪里开始。到目前为止,我知道闰年是4
可以整除的任何数字,但不能被100
整除(除非它也可以被400整除)。
答案 0 :(得分:1)
也许这会有所帮助。
year = int(input("Input year: "))
if year % 4 == 0:
print("Year is leap.")
if year % 100 == 0 and year % 400 != 0:
print("Year is common.")
else:
print("Year is common.")
答案 1 :(得分:0)
您可以做的是在代码顶部插入此功能:
def leapyear(year, querytype='is'):
import calendar
querytype == case(querytype, 'lowercase')
if querytype == 'is':
return calendar.isleap(year)
elif querytype == 'closest':
return year % 4
然后,要检查一年是否为闰年,请键入leapyear(THE_YEAR)’ and it will return
True or
False。这是如何使用它的另一个例子
def leapyear(year, querytype='is'):
import calendar
querytype == querytype.lower()
if querytype == 'is':
return calendar.isleap(year)
elif querytype == 'closest':
return year % 4
yeartocheck = input('Enter A Year. Your Answer: ')
if leapyear(yeartocheck) == True:
print('It Is A Leap Year')
else:
print('It Is Not A Leap Year')
通过我在此答案顶部提供的功能,您还可以通过键入leapyear(THE_YEAR, 'closest')
答案 2 :(得分:0)
我也是python初学者。我尽力了,看看是否行得通。
def isLeap(year):
tyear = year
if tyear % 4 == 0:
tyear = year
if tyear % 100 == 0:
tyear = year
if tyear % 400 == 0:
print('Leap Year')
else:
print('Not a Leap Year')
else:
print('Leap Year')
else:
print('Not a Leap Year')
isLeap(2004) # Returns Leap Year
isLeap(2005) # Returns Not a Leap Year
答案 3 :(得分:0)
以前的大多数答案都是正确的,这是给猫皮的另一种方法:
数学逻辑基于此full credit
year = 2024
if year % 4 != 0:
return False
elif year % 100 != 0:
return True
elif year % 400 != 0:
return False
else:
return True
这是测试用例
def checkit(year):
if year % 4 != 0:
return False
elif year % 100 != 0:
return True
elif year % 400 != 0:
return False
else:
return True
x = range(2013,2027)
for n in x:
print(n,checkit(n))
答案 4 :(得分:0)
这就是闰年的方法:)
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
print(year, "is leap!")
其他: 打印(年,“不是闰年!”)
答案 5 :(得分:-1)
如果你知道什么是闰年,那么你就知道如何用Python写这个。
你说的是闰年?给定一年,如果它可以被4整除,而不是100,那么这是真的。你如何检查一个数字是否可以被另一个数字整除?模块化算术。 x%y给出整数除法的余数(想想三等算术)。
所以对于一个int,这可以检查它是否可被4整除:
<b>Soaking time</b>: 3 hours <br>
<b>Preparation time:</b> 15 mins <br>
<b>Fermenting time:</b> 8 hours <br>
<span><b>Cooking time:</b> 45 mins<br><br></span>
1. Soak the urad dal (split black lentils) and fenugreek seeds (methi) together in water for 3 hours and drain. <br>
2. Soak the parboiled rice in water for 3 hours and drain. <br>
3. Blend urad dal and fenugreek seeds together in a mixer till smooth and frothy (add water little by little as required) remove and keep aside. <br>
4. Blend the rice in a mixer till smooth. Remove and keep aside. <br>
5. Combine the urad dal paste and rice paste together in a bowl and keep aside to ferment overnight. <br>
6. Once the batter is fermented, add salt to the batter and mix well. <br>
7. Put spoonfuls of the batter into greased idli moulds and steam for 10 to 12 minutes. <br>
8. Serve at room temperature. <br>
除此之外,还应该对闰年进行全面检查。希望这足以让你开始。