有没有什么方法可以从一个函数中返回两个对象而不生成元组?

2024-03-29 13:46:27 发布

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

我正在做一个基本的日期转换器,我需要更新的日期,每次用户输入一个无效的日期,并要求再次输入。从下面的函数中,我需要返回对象dayyear。你知道吗

def day_valid (month, dates, feb_day, month_days):

    day = int(dates[2:4])

    while month_days == 31 and day > 31:
        print ("Invalid day input.")
        print()
        dates = input_date()
        day = int(dates[2:4])

        if month_days == 31 and day < 32:
            break

    while month_days == 30 and day > 30:
        print ("Invalid day input.")
        print()
        dates = input_date()
        day = int(dates[2:4])

        if month_days == 30 and day < 31:
            break

    while month_days == feb_day and day > feb_day:
        print ("Invalid day input.")
        print()
        dates = input_date()
        day = int(dates[2:4])

        if month_days == feb_day and day <= feb_day:
            break


    return day

当用户以MMDDYYYY格式键入00102002时,没有月份。因此提示用户再次输入,输入01102005。代码仍然显示日期为2002年1月10日,而不是2005年1月10日。你知道吗

如果有人需要对代码进行说明,请询问!你知道吗

我的主要职能是:

def main():

    loop = "Y"

    print()
    print("Welcome to Date Converter!")
    print()

    while loop.upper () == "Y" :
        dates = input_date()

        year = int(dates[4:])

        month = month_valid(dates)
        feb_day = feb_days(year)

        month_days = month_Days(month, feb_day)

        day = day_valid(month, dates, feb_day, month_days)


        month_str = month_names(month)

        print()
        print("The date is " + str(day) + " " + month_str + " " + str(year))
        loop = str(input ("Do you want to re-run this program? Y/N: "))


main()

Tags: and用户inputdatedaysyearfebint
1条回答
网友
1楼 · 发布于 2024-03-29 13:46:27

这听起来首先像一个XY Problem:有人想做X,并提出了一个需要做Y的解决方案。他们需要Y的帮助,所以请求帮助做Y。然而,结果发现Y不是一个合适的解决方案。通过认识到XY问题并询问如何代替X,这个人得到了更好的帮助并对X有了更多的了解

XY问题也常常看起来像是可疑的家庭作业问题,因为这些问题的形式往往是“写一个程序,做X,做Y”。你知道吗

你可以提出一个问题,你想做X,并试图用Y来解决它

不管怎样,这就是为什么你可能会得到低努力的答案。我会努力的:)

不管怎样,继续问这个问题:)

有一种可读性实践认为元组是有害的,因为您不知道元组中项目的用途。考虑创建一个对象来保存对象,每个对象都有自己的属性,然后返回该属性。你知道吗

既然您声明需要dayyear返回:

class DayAndYear(object):
    def __init__(self, day, year):
        self.day = day
        self.year = year

这就是你不做元组的方法,它增加了你的程序的可读性,就像现在这样。你知道吗

现在,我们来回答一个未说明的问题:

  • 不知道month_valid做什么
  • 假设feb_days返回给定年份2月份的天数
  • 假设month_Days返回给定月份中非二月的天数

似乎需要一个函数来检查字符串是否是有效的MMDDYYYY字符串。你知道吗

def is_valid_date(s):
    """Checks if the given date is a valid MMDDYYYY string.

    Args:
        s (str): A date to check.
    Returns:
        bool: True if the date is valid, False otherwise.
    """
    if len(s) != 8:
        return False

    try:
        date = int(s[:2])
        month = int(s[2:4])
        year = int(s[4:])
    except ValueError:
        return False

    if month < 1 and month > 12:
        return False

    if month == 2:
        days_in_month = days_in_february(year)
    else:
        days_in_month = days_in_month(month)

    return date >= 1 and date <= days_in_month

def print_date(s):
    """Prints the given MMDDYYYY date, assuming it has already been checked for validity.

    Args:
        s (str): A date to print.
    """
    print("The date is {:d} {:s} {:d}.".format(
        int(s[2:4]), month_name(int(s[:2])), int(s[4:])))

我想重点介绍一些让你的程序读起来更好的一般技巧:

  • 我们不知道X。一个适定的问题是关于程序输入和输出的规范。你知道吗
  • 我使用了详细的、可读的函数名。你知道吗
  • 我使用了函数注释,包括arg、arg类型和返回值,这样就不用猜测事情会发生什么。你知道吗
  • 我选择了在检查有效性和打印一个已经有效的字符串之间进行分割。你可以把它们结合起来。也可以返回字符串而不是打印日期,如果日期无效,则返回sentinel值None。你知道吗
  • 不要计算太多。注意提前归还。你知道吗
  • 毫无疑问,有些库函数可以做到这一点,但我假设您不想使用任何库函数。你知道吗

简短的关键概念:

  • 可读性:程序应该和你的母语散文一样容易阅读。你知道吗
  • 可读性:函数名应该是描述性的。你知道吗
  • 可读性:注释代码。你知道吗
  • 可读性:为函数选择一致的格式并坚持使用(“month_Days”vs“feb_days”)
  • 效率:早点回来。你知道吗
  • 可测试性:详细说明你的程序在输入和输出方面做了什么,给出好的和坏的输入的例子。你知道吗
  • 有效性:使用库函数。你知道吗
  • Stackoverflowness:考虑您的问题是否是XY问题。你知道吗

相关问题 更多 >