我一直试图找到一种方法来完成标题中的内容,而不使用Python中任何导入的日历/日期时间库。顶部几乎没有功能来检查年份是否是闰年,我希望能够在打印给定二月份的天数时参考,但是我不太清楚如何这样做。 (我已经猜到了输出.bla bla)
到目前为止,我已经想出了类似的东西,这应该说清楚我想要做什么,但我仍然对Python有点新意,所以我想要一些提示/帮助来修复我的代码为了这项任务。
# A function to determine if a year is a leap year.
# Do not change this function.
def is_leap_year (year):
return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)
# You should complete the definition of this function:
def days_in_month(month, year):
if month == 'September' or month == 'April' or month == 'June' or month == 'November'
print 30
elseif month == 'January' or month == 'March' or month == 'May' or month== 'July' or month == 'August' or month == 'October'\
or month== 'December'
print 31
elseif month == 'February' and output.is_leap_year = True
print 29
elseif month == 'February' and output.is_leap_year = False
print 28
else print 'Blank'
好的我已经修复了我的代码,似乎每个月输出正确的数据,但是2月:
# A function to determine if a year is a leap year.
# Do not change this function.
def is_leap_year (year):
return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)
# You should complete the definition of this function:
def days_in_month(month, year):
if month in ['September', 'April', 'June', 'November']:
print 30
elif month in ['January', 'March', 'May', 'July', 'August','October','December']:
print 31
elif month == 'February' and is_leap_year == True:
print 29
elif month == 'February' and is_leap_year == False:
print 28
要解决2月份输出的任何提示吗?
编辑:只需要在引用第一个函数时添加参数年份。以下是100%工作代码供将来参考:
# A function to determine if a year is a leap year.
# Do not change this function.
def is_leap_year(year):
return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)
# You should complete the definition of this function:
def days_in_month(month, year):
if month in ['September', 'April', 'June', 'November']:
print 30
elif month in ['January', 'March', 'May', 'July', 'August','October','December']:
print 31
elif month == 'February' and is_leap_year(year) == True:
print 29
elif month == 'February' and is_leap_year(year) == False:
print 28
else:
return None
答案 0 :(得分:2)
代码中出现一些语法错误:
def days_in_month(month,year)
之前不应有空格。 Python使用缩进来分隔代码块。这是您在评论中给出的错误。elseif
,它应该是elif
output.is_leap_year = True
,应为is_leap_year(year) == True
。 False
部分也应该更改。在if
语句和else
之后应该有:
,例如
if month == 'September' or month == 'April' or month == 'June' or month == 'November':
print 30
elif month == 'January' or month == 'March' or month == 'May' or month== 'July' or month == 'August' or month == 'October' or month== 'December':
print 31
elif month == 'February' and is_leap_year(year) == True:
print 29
elif month == 'February' and is_leap_year(year) == False:
print 28
else:
print 'Blank'
答案 1 :(得分:2)
更pythonic的方法是在字典中定义映射,然后简单地从字典中检索值。
尝试一下:
days_in_month_dict = {"January": 31, "February": 28,
"March": 31, "April": 30,
"May": 31, "June": 30,
"July": 31, "August": 31,
"September": 30, "October": 31,
"November": 30, "December": 31}
def is_leap_year(year):
return (year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)
def days_in_month(year, month):
if is_leap_year(year) and month == "February":
return 28
try:
#attempt to get value from dictionary
return days_in_month_dict[month]
except KeyError:
#key does not exist, so we caught the error
return None
答案 2 :(得分:0)
Rectangle {
id: calendarPopUp
visible: false;
border.width: 0
color: "#ffffff"
anchors.left: parent.left
anchors.top: parent.top
width: parent.width
z: 10;
VMComboBox{
id: yearPicker
width: parent.width
//vmModel: ["1999", "2000"]
vmModel: {
var ans = [];
var cyear = 2018;
var fyear = cyear - 120;
for (var i = cyear; i >= fyear; i--){
ans.push(i)
}
calendar.minimumDate = new Date(fyear, 0, 1);
calendar.maximumDate = new Date(cyear, 0, 1);
return ans;
}
font.family: viewHome.robotoR.name
anchors.top: parent.top
anchors.topMargin: 2
anchors.horizontalCenter: parent.horizontalCenter
onCurrentIndexChanged: {
var year = yearPicker.currentText;
console.log("Changing visible year to: " + year);
calendar.visibleYear = parseInt(year);
}
}
Calendar {
id: calendar
width: parent.width
anchors.top: yearPicker.bottom
anchors.horizontalCenter: parent.horizontalCenter
}
}
答案 3 :(得分:0)
month = int (input ('month (1-12): '))
if month < 13:
if month == 2:
year = int (input ('year: '))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print ('29')
else:
print ('28')
else:
print ('29')
else:
print ('28')
elif month >= 8:
if month % 2 == 0:
print ('31')
else:
print ('30')
elif month % 2 == 0:
print ('30')
else:
print ('31')
else:
print ('Only 1-12 accepted')
答案 4 :(得分:0)
month=input("month")
year=int(input("year"))
if year%4==0:
year=('leap year')
if month in ['September', 'April', 'June', 'November']:
print ("30")
elif month in ['January', 'March', 'May', 'July', 'August','October','December']:
print ("31")
elif month == 'February' and year == "leap year":
print ("29")
elif month == 'February' and year != "leap year":
print ("28")
else:
print("none")