Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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程序中 导入数学 重复=正确 日期=原始输入(“以MM/DD/YYYY格式输入日期”)#提示用户输入 重复时: date_month=date[0:2]#获取输入的月份,并为可变的date_month保留其值 date_day=date[3:5]#获取输入的日期并将其值保留为可变日期 date_year=date[6:10]#获取输入的年份,并将其值保留为可变日期_year 如果00_Python_Python 2.7 - Fatal编程技术网

我需要帮助将闰年功能添加到我的python程序中 导入数学 重复=正确 日期=原始输入(“以MM/DD/YYYY格式输入日期”)#提示用户输入 重复时: date_month=date[0:2]#获取输入的月份,并为可变的date_month保留其值 date_day=date[3:5]#获取输入的日期并将其值保留为可变日期 date_year=date[6:10]#获取输入的年份,并将其值保留为可变日期_year 如果00

我需要帮助将闰年功能添加到我的python程序中 导入数学 重复=正确 日期=原始输入(“以MM/DD/YYYY格式输入日期”)#提示用户输入 重复时: date_month=date[0:2]#获取输入的月份,并为可变的date_month保留其值 date_day=date[3:5]#获取输入的日期并将其值保留为可变日期 date_year=date[6:10]#获取输入的年份,并将其值保留为可变日期_year 如果00,python,python-2.7,Python,Python 2.7,就这么简单: import math repeat = True date = raw_input('Enter a date in the format MM/DD/YYYY') #Prompts user to input while repeat: date_month = date[0:2] #Takes the input's month and keeps its value for the varible date_month date

就这么简单:

import math    
repeat = True    
date = raw_input('Enter a date in the format MM/DD/YYYY') #Prompts user to input    

while repeat:
   date_month = date[0:2] #Takes the input's month and keeps its value for the varible date_month    
   date_day = date[3:5] #Takes the input's day and keeps its value for the varible date_day    
   date_year = date[6:10] #Takes the input's year and keeps its value for the varible date_year       
   if 00 < int(date_month) < 13:    
      if 00 < int(date_day) < 32:    
         if 0000 < int(date_year) < 2017:    
               date = raw_input('The date you entered is valid, enter another date in the format MM/DD/YYYY')               
   else:    
      date = raw_input('invalid date found! Please enter another date in the format MM/DD/YYYY')

这个问题有更多的问题,而不仅仅是今年是否是闰年@Dex'ter解决方案确实会告诉你们一年是闰年还是非闰年,但问题是你们最终不能要求那个。因此,您的第一个问题应该是用户输入的日期是闰年:

>>> import calendar
>>> print(calendar.isleap(1999))
False
>>> print(calendar.isleap(2004))
True
,然后是代码运行的月份。然后你)在一天中会有另一个问题

 if calendar.isleap(date_year):

编辑:顺便说一下,最后一个函数也支持闰年。

滚动您自己的解析器代码是愚蠢的:

导入日期时间
重复=正确
datestr=原始输入('以MM/DD/YYYY格式输入日期')
重复时:
尝试:
#解析为datetime,然后转换为date,因为这就是您所使用的全部
date=datetime.datetime.strtime(datestr,“%m/%d/%Y”).date()
除值错误外:
通过#在通用代码中处理错误日期
其他:
如果0000<日期.年份<2017年:
datestr=raw_input('您输入的日期有效,请以MM/DD/YYYY'格式输入另一个日期')
继续#绕过无效日期输入通用代码
#例外年和无效年都将属于此通用代码
datestr=raw_input('找到无效日期!请以MM/DD/YYYY'格式输入另一个日期')
显然,正如所写的,在任何条件下,这实际上都不会终止循环,但您的原始代码也不会。这里的优点是,
strtime
起重;除了,它通过一次
尝试
/
来验证原始代码之外的更多内容,并在不进行特殊检查的情况下逐月处理棘手的事情,如每天。您可以访问它像以前一样解析的
date
对象的
year
month
day
属性,作为Python本机
int
s,不需要进行单独的
int
转换


请注意,如果您想使用与区域设置相适应的日期表示,您选择的格式恰好与
en_US
locale完全匹配,您可以通过使用
datetime.datetime.strtime(datestr,“%x”)使其对非美国用户更具可移植性。
(虽然您必须使
原始输入
提示动态匹配);这与
'%m/%d/%Y'
对于
en\u US
的工作原理相同,但会切换排序和分隔符,例如,德国语言环境,这将使其相当于
'%d.%m.%Y'

我重写了您的程序。我希望它能有所帮助

import datetime

repeat = True    
datestr = raw_input('Enter a date in the format MM/DD/YYYY')

while repeat:
    try:
        # Parse to datetime, then convert to date since that's all you use
        date = datetime.datetime.strptime(datestr, '%m/%d/%Y').date()
    except ValueError:
        pass  # Handle bad dates in common code
    else:
        if 0000 < date.year < 2017:
            datestr = raw_input('The date you entered is valid, enter another date in the format MM/DD/YYYY')
            continue  # Bypass invalid date input common code

    # Both exception and invalid years will fall through to this common code
    datestr = raw_input('invalid date found! Please enter another date in the format MM/DD/YYYY')

请努力格式化您的问题。此外,源代码本身也不是问题。标题说明了一切:XT标题说明您需要帮助。每个参加SO的人都需要帮助。标题说明python程序。您使用了python标记,所以我们已经知道了这一点。以何种方式添加闰年功能?确定闰年,使用状态做一些不同的事情,忽略闰年,查找闰年,存储闰年?你没有解释你所说的闰年功能是什么意思。我明白你的意思了hehe抱歉我的错误我认为你不理解这个问题。
我需要帮助将闰年功能添加到我的python程序中
你从中理解了什么?我只是提供了一个替代他所要求的^^
 >>> from calendar import monthrange
 >>> monthrange(2011, 2)
import datetime

repeat = True    
datestr = raw_input('Enter a date in the format MM/DD/YYYY')

while repeat:
    try:
        # Parse to datetime, then convert to date since that's all you use
        date = datetime.datetime.strptime(datestr, '%m/%d/%Y').date()
    except ValueError:
        pass  # Handle bad dates in common code
    else:
        if 0000 < date.year < 2017:
            datestr = raw_input('The date you entered is valid, enter another date in the format MM/DD/YYYY')
            continue  # Bypass invalid date input common code

    # Both exception and invalid years will fall through to this common code
    datestr = raw_input('invalid date found! Please enter another date in the format MM/DD/YYYY')
import math
from datetime import datetime
repeat = True    
date = raw_input('Enter a date in the format MM/DD/YYYY :') 
while repeat:
    try:
        datetime.strptime(date, "%m/%d/%Y")
        date = raw_input('The date you entered is valid, enter another date in the format MM/DD/YYYY :')
    except ValueError:
        date = raw_input('invalid date found! Please enter another date in the format MM/DD/YYYY :')