Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中转换一个月的天数?_Python_Date_Calendar - Fatal编程技术网

在Python中转换一个月的天数?

在Python中转换一个月的天数?,python,date,calendar,Python,Date,Calendar,Python新手,我正在编写一个程序,让用户输入月份(以数字表示,而不是单词-例如“3”而不是“3”)和年份(如“2014”)。我希望程序显示输入月份和年份的天数。因此,如果用户输入第3个月和2005年,则应显示: 2005年3月有31天 这是我的密码: def enteredMonth(): month = int(input("Enter a month in terms of a number: ")) return month def enteredYear():

Python新手,我正在编写一个程序,让用户输入月份(以数字表示,而不是单词-例如“3”而不是“3”)和年份(如“2014”)。我希望程序显示输入月份和年份的天数。因此,如果用户输入第3个月和2005年,则应显示:

2005年3月有31天

这是我的密码:

def enteredMonth():
    month = int(input("Enter a month in terms of a number: "))
    return month

def enteredYear():
    year = int(input("Enter a year: "))
    return int(year)

def leapYear(year):
    if year % 4 == 0:
        return True
    else:
        return False

def numberOfDays(month):
    if enteredMonth() == 1:
        month = "January"
        print ("31")
    elif enteredMonth() == 2:
        month = "February"
        print ("28")
    elif enteredMonth() == 2 and leapYear() == true:
        month = "February"
        print ("29")
    elif enteredMonth() == 3:
        month = "March"
        print ("31")
    elif enteredMonth() == 4:
        month = "April"
        print ("30")
    elif enteredMonth() == 5:
        month = "May"
        print ("31")
    elif enteredMonth() == 6:
        month = "June"
        print ("30")
    elif enteredMonth() == 7:
        month = "July"
        print ("31")
    elif enteredMonth() == 8:
        month = "August"
        print ("31")
    elif enteredMonth() == 9:
        month = "September"
        print ("30")
    elif enteredMonth() == 10:
        month = "October"
        print ("31")
    elif enteredMonth() == 11:
        month = "November"
        print ("30")
    elif enteredMonth() == 12:
        month = "December"
        print ("31")
    else:
        print("Please enter a valid month")

def main():
   enteredMonth()
   enteredYear()
   leapYear(year)
   numberOfDays(month)
   print(month, enteredYear(), "has", numberOfDays(month) , "days")

if __name__ == '__main__': 
   main()
问题是,我没有得到正确的格式,而是得到了如下结果:

3 2005年没有一天


请帮忙!非常感谢。

在函数
numberOfDays()
中,您需要返回天数,而不仅仅是打印它。由于它不返回任何内容,因此在
main()
中打印
None
。也就是说,您需要在
numberOfDays()中输入此行:

其中,
num_days
设置为给定月份的天数。事实上,
numberOfDays()
事件根本不应该打印任何内容。只需返回天数即可

另外,为什么需要在
numberOfDays()中调用这么多
enteredMonth()
?您只需检查输入参数
month

################################################################
# OR Another way to achieve same thing
def numberOfDays(month, year):
    daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    if month > len(daysInMonths) or year < 0 or month < 0:
        return "Please enter a valid month/year"

    # Here, only divisible by 4 as leap-year but there are many 
    # more conditions for a leap year which you can add in expression
    return daysInMonths[month-1] + int((year % 4) == 0 and month == 2)

def main():
   year = int(input("Enter a year: "))
   month = int(input("Enter a month in terms of a number: "))
   print(month, year, "has", numberOfDays(month, year) , "days")

if __name__ == '__main__': 
   main()

################################################################
# OR using standard library
from calendar import monthrange

def main():
   year = int(input("Enter a year: "))
   month = int(input("Enter a month in terms of a number: "))
   if year < 0 or month < 0:
    print "Pleae enter a valid month/year"
    return
   print(month, year, "has", monthrange(year, month)[1] , "days")

if __name__ == '__main__': 
   main()

main()
内部,
enteredMonth()
enteredYear()
不会设置任何变量。您需要在函数
numberOfDays()
中执行类似于
month=enteredMonth()
的操作,您需要返回天数,而不仅仅是打印它。由于它不返回任何内容,因此在
main()
中打印
None
。也就是说,您需要在
numberOfDays()中输入此行:

其中,
num_days
设置为给定月份的天数。事实上,
numberOfDays()
事件根本不应该打印任何内容。只需返回天数即可

另外,为什么需要在
numberOfDays()中调用这么多
enteredMonth()
?您只需检查输入参数
month

################################################################
# OR Another way to achieve same thing
def numberOfDays(month, year):
    daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    if month > len(daysInMonths) or year < 0 or month < 0:
        return "Please enter a valid month/year"

    # Here, only divisible by 4 as leap-year but there are many 
    # more conditions for a leap year which you can add in expression
    return daysInMonths[month-1] + int((year % 4) == 0 and month == 2)

def main():
   year = int(input("Enter a year: "))
   month = int(input("Enter a month in terms of a number: "))
   print(month, year, "has", numberOfDays(month, year) , "days")

if __name__ == '__main__': 
   main()

################################################################
# OR using standard library
from calendar import monthrange

def main():
   year = int(input("Enter a year: "))
   month = int(input("Enter a month in terms of a number: "))
   if year < 0 or month < 0:
    print "Pleae enter a valid month/year"
    return
   print(month, year, "has", monthrange(year, month)[1] , "days")

if __name__ == '__main__': 
   main()

main()
内部,
enteredMonth()
enteredYear()
不会设置任何变量。您需要在函数
numberOfDays()
中执行类似于
month=enteredMonth()
的操作,您需要返回天数,而不仅仅是打印它。由于它不返回任何内容,因此在
main()
中打印
None
。也就是说,您需要在
numberOfDays()中输入此行:

其中,
num_days
设置为给定月份的天数。事实上,
numberOfDays()
事件根本不应该打印任何内容。只需返回天数即可

另外,为什么需要在
numberOfDays()中调用这么多
enteredMonth()
?您只需检查输入参数
month

################################################################
# OR Another way to achieve same thing
def numberOfDays(month, year):
    daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    if month > len(daysInMonths) or year < 0 or month < 0:
        return "Please enter a valid month/year"

    # Here, only divisible by 4 as leap-year but there are many 
    # more conditions for a leap year which you can add in expression
    return daysInMonths[month-1] + int((year % 4) == 0 and month == 2)

def main():
   year = int(input("Enter a year: "))
   month = int(input("Enter a month in terms of a number: "))
   print(month, year, "has", numberOfDays(month, year) , "days")

if __name__ == '__main__': 
   main()

################################################################
# OR using standard library
from calendar import monthrange

def main():
   year = int(input("Enter a year: "))
   month = int(input("Enter a month in terms of a number: "))
   if year < 0 or month < 0:
    print "Pleae enter a valid month/year"
    return
   print(month, year, "has", monthrange(year, month)[1] , "days")

if __name__ == '__main__': 
   main()

main()
内部,
enteredMonth()
enteredYear()
不会设置任何变量。您需要在函数
numberOfDays()
中执行类似于
month=enteredMonth()
的操作,您需要返回天数,而不仅仅是打印它。由于它不返回任何内容,因此在
main()
中打印
None
。也就是说,您需要在
numberOfDays()中输入此行:

其中,
num_days
设置为给定月份的天数。事实上,
numberOfDays()
事件根本不应该打印任何内容。只需返回天数即可

另外,为什么需要在
numberOfDays()中调用这么多
enteredMonth()
?您只需检查输入参数
month

################################################################
# OR Another way to achieve same thing
def numberOfDays(month, year):
    daysInMonths = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    if month > len(daysInMonths) or year < 0 or month < 0:
        return "Please enter a valid month/year"

    # Here, only divisible by 4 as leap-year but there are many 
    # more conditions for a leap year which you can add in expression
    return daysInMonths[month-1] + int((year % 4) == 0 and month == 2)

def main():
   year = int(input("Enter a year: "))
   month = int(input("Enter a month in terms of a number: "))
   print(month, year, "has", numberOfDays(month, year) , "days")

if __name__ == '__main__': 
   main()

################################################################
# OR using standard library
from calendar import monthrange

def main():
   year = int(input("Enter a year: "))
   month = int(input("Enter a month in terms of a number: "))
   if year < 0 or month < 0:
    print "Pleae enter a valid month/year"
    return
   print(month, year, "has", monthrange(year, month)[1] , "days")

if __name__ == '__main__': 
   main()
main()
内部,
enteredMonth()
enteredYear()
不会设置任何变量。您需要执行类似于
month=enteredMonth()

################################################################
#或者用另一种方法来达到同样的目的
def天数(月、年):
daysInMonths=[31,28,31,30,31,30,31,31,30,31,30,31]
如果月>月(日/月)或年<0或月<0:
return“请输入有效的月份/年份”
#在这里,闰年只能被4整除,但有很多
#可以在表达式中添加闰年的更多条件
返回日期月[1月]+int((第%4年)=0,月==2)
def main():
年份=整数(输入(“输入年份:”)
month=int(输入(“以数字形式输入月份:”)
打印(月,年,“已”,numberOfDays(月,年),“天”)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
################################################################
#或者使用标准库
从日历导入monthrange
def main():
年份=整数(输入(“输入年份:”)
month=int(输入(“以数字形式输入月份:”)
如果年份<0或月份<0:
打印“请输入有效的月份/年份”
返回
打印(月,年,“有”,蒙特兰奇(年,月)[1],“天”)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
################################################################
#或者用另一种方法来达到同样的目的
def天数(月、年):
daysInMonths=[31,28,31,30,31,30,31,31,30,31,30,31]
如果月>月(日/月)或年<0或月<0:
return“请输入有效的月份/年份”
#在这里,闰年只能被4整除,但有很多
#可以在表达式中添加闰年的更多条件
返回日期月[1月]+int((第%4年)=0,月==2)
def main():
年份=整数(输入(“输入年份:”)
month=int(输入(“以数字形式输入月份:”)
打印(月,年,“已”,numberOfDays(月,年),“天”)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
################################################################
#或者使用标准库
从日历导入monthrange
def main():
年份=整数(输入(“输入”)