If statement 试图找出如何在Python 3.3.0中正确使用while循环

If statement 试图找出如何在Python 3.3.0中正确使用while循环,if-statement,python-3.x,while-loop,If Statement,Python 3.x,While Loop,我是Python的一名优秀的noob,我正在编写一个程序,输出给定日期是否为有效的日历日期。我相信有更优雅的方式来做这件事 在这一点上,我试图找出如何添加一个变量来创建一个while循环,该循环将处理日期,如果它是闰年或不是闰年。不过,所有建议都很受欢迎 我已经将代码尝试中的问题区域放在了里面。以下是我目前掌握的代码: def main(): print("This program tests the validity of a given date") date = (input("Plea

我是Python的一名优秀的noob,我正在编写一个程序,输出给定日期是否为有效的日历日期。我相信有更优雅的方式来做这件事

在这一点上,我试图找出如何添加一个变量来创建一个while循环,该循环将处理日期,如果它是闰年或不是闰年。不过,所有建议都很受欢迎

我已经将代码尝试中的问题区域放在了里面。以下是我目前掌握的代码:

def main():
print("This program tests the validity of a given date")

date = (input("Please enter a date (mm/dd/yyyy): "))
month, day, year = date.split("/")
month = int(month)
day = int(day)
year = int(year)
Mylist31 = [1, 3, 5, 7, 8, 10, 12]
Mylist30 = [4, 6, 9, 11]

#Calculates whether input year is a leap year or not
if year >= 100 and year % 4 == 0 and year % 400 == 0:
    <it is a leap year>
elif year >= 0 and year <100 and year % 4 == 0:
    <it is a leap year>
else: 
    <it is not leapyear>


while <it is a leapyear>:
    if month in Mylist31 and day in range(1, 32):
        print("Valid date")
    elif month in Mylist30 and day in range(1,31):
        print("Valid date")
    elif month == 2 and day in range(1,30):
        print("Valid date")
    else:
        print("Not a Valid date")  
while <it is not a leapyear>:
etc...
def main():
打印(“此程序测试给定日期的有效性”)
日期=(输入(“请输入日期(mm/dd/yyyy):”)
月、日、年=日期分割(“/”)
月=整数(月)
日=整数(日)
年份=整数(年)
Mylist31=[1,3,5,7,8,10,12]
Mylist30=[4,6,9,11]
#计算输入年份是否为闰年
如果年份>=100,年份%4==0,年份%400==0:

如果year>=0和year我已经稍微完成了您的代码。希望从那以后,你能继续朝着你想要的方向改进

def main():
    print("This program tests the validity of a given date")

    date = (raw_input("Please enter a date (mm/dd/yyyy): "))
    month, day, year = date.split("/")
    month = int(month)
    day = int(day)
    year = int(year)
    Mylist31 = [1, 3, 5, 7, 8, 10, 12]
    Mylist30 = [4, 6, 9, 11]

    ##Calculates whether input year is a leap year or not
    if year >= 100 and year % 4 == 0 and year % 400 == 0:
        is_leap_year = True
    elif year >= 0 and year <100 and year % 4 == 0:
        is_leap_year = True
    else:
        is_leap_year = False

    if is_leap_year:
        if month in Mylist31 and day in range(1, 32):
            print("Valid date")
        elif month in Mylist30 and day in range(1,31):
            print("Valid date")
        elif month == 2 and day in range(1,30):
            print("Valid date")
        else:
            print("Not a Valid date")
    else:
        #TODO: validate non-leap-year date
        pass

main()
def main():
打印(“此程序测试给定日期的有效性”)
日期=(原始输入(“请输入日期(mm/dd/yyyy):”)
月、日、年=日期分割(“/”)
月=整数(月)
日=整数(日)
年份=整数(年)
Mylist31=[1,3,5,7,8,10,12]
Mylist30=[4,6,9,11]
##计算输入年份是否为闰年
如果年份>=100,年份%4==0,年份%400==0:
闰年是真的吗

如果year>=0和year我已经稍微完成了您的代码。希望从那以后,你能继续朝着你想要的方向改进

def main():
    print("This program tests the validity of a given date")

    date = (raw_input("Please enter a date (mm/dd/yyyy): "))
    month, day, year = date.split("/")
    month = int(month)
    day = int(day)
    year = int(year)
    Mylist31 = [1, 3, 5, 7, 8, 10, 12]
    Mylist30 = [4, 6, 9, 11]

    ##Calculates whether input year is a leap year or not
    if year >= 100 and year % 4 == 0 and year % 400 == 0:
        is_leap_year = True
    elif year >= 0 and year <100 and year % 4 == 0:
        is_leap_year = True
    else:
        is_leap_year = False

    if is_leap_year:
        if month in Mylist31 and day in range(1, 32):
            print("Valid date")
        elif month in Mylist30 and day in range(1,31):
            print("Valid date")
        elif month == 2 and day in range(1,30):
            print("Valid date")
        else:
            print("Not a Valid date")
    else:
        #TODO: validate non-leap-year date
        pass

main()
def main():
打印(“此程序测试给定日期的有效性”)
日期=(原始输入(“请输入日期(mm/dd/yyyy):”)
月、日、年=日期分割(“/”)
月=整数(月)
日=整数(日)
年份=整数(年)
Mylist31=[1,3,5,7,8,10,12]
Mylist30=[4,6,9,11]
##计算输入年份是否为闰年
如果年份>=100,年份%4==0,年份%400==0:
闰年是真的吗

elif year>=0,而year我看不出需要一个while循环。您正在检查给定日期是否正确。要么是,要么不是,你可以马上知道。而且,如果是闰年,这是一个无止境的循环(条件永远不会改变)。@我知道这里有一个无止境的循环。请看,在闰年中,2月份有一个额外的日子,因此,根据年份的不同,给定的日期是否正确会发生变化。当然,你可以通过看它来判断,但我正试图在这里编写一个程序,程序不能没有说明就判断。谢谢(在这里插入蟋蟀)。我试图通过指出第一步来帮助您:摆脱不正确的while循环。当我给你宝贵的时间免费帮助你的时候,你却抱怨和谈论蟋蟀?@我又看了一遍,你确实帮了我。对不起,我太粗鲁了。这不是借口,但我一直盯着这个代码看太久了,没有睡觉。我只是使用了你建议的所有if,elif,else语句,效果很好。我仍然想知道如何使它与while语句一起工作,即使它是多余的。我非常感谢你的回答。当我重新审视一切时,它确实有所帮助。@user2146060接受了道歉。我看不出需要一段时间。您正在检查给定日期是否正确。要么是,要么不是,你可以马上知道。而且,如果是闰年,这是一个无止境的循环(条件永远不会改变)。@我知道这里有一个无止境的循环。请看,在闰年中,2月份有一个额外的日子,因此,根据年份的不同,给定的日期是否正确会发生变化。当然,你可以通过看它来判断,但我正试图在这里编写一个程序,程序不能没有说明就判断。谢谢(在这里插入蟋蟀)。我试图通过指出第一步来帮助您:摆脱不正确的while循环。当我给你宝贵的时间免费帮助你的时候,你却抱怨和谈论蟋蟀?@我又看了一遍,你确实帮了我。对不起,我太粗鲁了。这不是借口,但我一直盯着这个代码看太久了,没有睡觉。我只是使用了你建议的所有if,elif,else语句,效果很好。我仍然想知道如何使它与while语句一起工作,即使它是多余的。我非常感谢你的回答,当我再看一眼的时候,它确实帮了我的忙。@user2146060接受道歉。