为什么今天的计数器产生错误的结果?

2024-04-25 13:58:01 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我是Python的初学者,目前正在PyCharm上使用python3.4.1。我最近做了一个项目,计算两个日期之间的天数,但有两个问题。你知道吗

def get_first_day():
    while True:
        try:
            print('First Date')
            day = int(input('Day:'))
            month = int(input('Month:'))
            year = int(input('Year:'))
            print(day, '/', month, '/', year)
            date = [day, month, year * 365]
            get_second_day(date)
        except ValueError:
            print('You were supposed to enter a date.')

 def get_second_day(date_1):
    while True:
       try:
           print('Second Date')
           day = int(input('Day:'))
           month = int(input('Month:'))
           year = int(input('Year:'))
           print(day, '/', month, '/', year)
           date = [day, month, year * 365]
           convert_dates_and_months(date_1, date)
       except ValueError:
           print('You were supposed to enter a date.')


def convert_dates_and_months(date_1, date_2):
    days_unfiltered = [date_1[0], date_2[0]]
    months_unfiltered = [date_1[1], date_2[1]]
    year = [date_1[2], date_2[2]]
    date_unfiltered = zip(days_unfiltered, months_unfiltered, year)
    for d, m, y in date_unfiltered:
        if m in [1, 3, 5, 7, 8, 10, 12]:
            a = 31
        elif m in [4, 6, 9, 11]:
            a = 30
        elif m in [2, 0] and int(y) % 4 is 0:
            a = 29
        else:
            a = 28
        m *= a
    days = list(filter(lambda x: 0 < x < (a + 1), days_unfiltered))
    months = list(filter(lambda x: 0 < x < 13, months_unfiltered))
    date_1 = [days[0], months[0], year[0]]
    date_2 = [days[1], months[1], year[1]]
    determine_date_displacement(date_1, date_2)


def determine_date_displacement(date_1, date_2):
    full_dates = zip(date_1, date_2)
    days = -1
    for k, v in full_dates:
        days += (int(v) - int(k))
    if days < 0:
        days *= -1
    print(days)


get_first_day()

第一个问题是计数器返回的两个日期之间的天数不正确。第二个原因是def get\u second\u day由于某些原因在结尾重复。我来告诉你我的意思:

First Date
Day:10
Month:09
Year:03
10 / 9 / 3

Second Date
Day:06
Month:06
Year:06
6 / 6 / 6

1087

Second Date
Day:

我知道在2003年9月10日到2006年6月6日之间有1000天的时间,但是这个项目的回报是1087天。你知道吗

如果有人能解释为什么这个项目返回一个不正确的数字,以及为什么它要求我填写第二次日期再次结束,那将是完美的。你知道吗

因为这是我的第一个问题,而且我是Python的初学者,所以对于在这个问题中看到的任何奇怪的措辞/糟糕的做法,我提前表示歉意。你知道吗


Tags: ininputgetdatedefdaysyearint
1条回答
网友
1楼 · 发布于 2024-04-25 13:58:01

问题1:

您的闰年计算已关闭:

闰年是years % 4 == 0,但只有闰年不是year % 100 == 0,除非 它们也是year % 400 == 0

2004,2008,2012 : leap year (%4==0, not %100==0)
1700,1800,1900 : no leap year (%4 == 0 , % 100 == 0 but not %400 == 0)
1200,1600,2000 : leap years (* 1200 theor. b/c gregorian cal start)

问题2:

在你的输入中,你将一年乘以365,而不检查闰年——他们通常有366天,但得到365天——这将导致在计算闰年的天数时缺少天数。你知道吗

问题3:

您有一个控制流问题:get_second_day()重复,因为您:

get_first_date()
    while without end:
        do smth
        call get_second_date(..)
             while without end:
                 do smth 
                 call some calculation functions
                     that calc and print and return with None 
                 back in get_second_date(), no break, so back to the beginning
                 of its while and start over forever - you are TRAPPED
  • break放在convert_dates_and_months(date_1, date)后面,放在get_second_day(..)里面来修复它

建议:

您可以通过减少get_first_day()get_second_day()之间的重复代码量来简化输入—这遵循干燥原则(Don'tRepeatYourself):

def getDate(text):
    while True:
        try:
            print(text)
            day = int(input('Day:'))
            month = int(input('Month:'))
            year = int(input('Year:'))
            print(day, '/', month, '/', year)
            return [day, month, year * 365]  # see Problem 2
        except ValueError:
            print('You were supposed to enter a date.')


def get_first_day():
    date1 = getDate("First Date")
    # rest of code omitted 

def get_second_day(date_1):
    date = getDate("Second Date")
    # rest of code omitted 

更好的解决方案是利用datetime and datettime-parsing,特别是如果你想处理输入验证和闰年估计,你需要更多的检查。你知道吗

使用datetime模块很简单:

import datetime

def getDate(text):
    while True:
        try:
            print(text)
            day = int(input('Day:'))
            month = int(input('Month:'))
            year = int(input('Year (4 digits):'))
            print(day, '/', month, '/', year)

            # this will throw error on invalid dates: 
            # f.e. 66.22.2871 or even (29.2.1977) and user
            # gets a new chance to input something valid
            return datetime.datetime.strptime("{}.{}.{}".format(year,month,day),"%Y.%m.%d")
        except (ValueError,EOFError):
            print('You were supposed to enter a valid date.')


def get_first_day():
    return getDate("First Date")

def get_second_day():
    return getDate("Second Date")

# while True: # uncomment and indent next lines to loop endlessly
first = get_first_day()     # getDate("First Date") and getDate("Second Date") 
second = get_second_day()   # directly would be fine IMHO, no function needed
print( (second-first).days) 

输出:

First Date
Day:10
Month:9
Year (4 digits):2003
10 / 9 / 2003
Second Date
Day:6
Month:6
Year (4 digits):2006
6 / 6 / 2006
1000 

很好的阅读:How to debug small programs (#1)-在它之后,至少可以将您带到控制流问题。你知道吗

相关问题 更多 >