You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python时间计算器函数重复执行及类型错误问题求助

时间计算器:函数重复执行与AttributeError问题修复

问题分析

1. 整数2的来源

你在time_calculator末尾调用weekday_calculator时参数顺序完全错误:

# 错误调用
weekday_calculator(new_hour, new_minute, new_am_pm, weekday, day_count)

而weekday_calculator的定义是:

def weekday_calculator(weekday, day_count, new_hour, new_minute, new_am_pm):

你把计算后的new_hour(此时值为2)传到了第一个参数weekday的位置,导致weekday变成整数2,后续执行weekday.lower()自然触发AttributeError。

2. 函数重复执行的原因

  • day_calculator内部已经调用了weekday_calculator和result_printer
  • time_calculator末尾又再次调用这两个函数
  • 同时weekday_calculator里也调用了result_printer,导致多重重复执行

另外还有几个隐性问题:

  • result_printer里的for循环会提前return,导致后续时间格式化和打印逻辑无法执行
  • adjust_weekday里的define_weekday变量未定义,传参错误
  • 用timeday_am和timeday_pm列表计算天数过于复杂,完全没必要

修复方案

1. 修正参数传递顺序

在time_calculator中,按照weekday_calculator的定义顺序传参:

weekday_calculator(weekday, day_count, new_hour, new_minute, new_am_pm)

2. 移除重复的函数调用

删除time_calculator末尾多余的weekday_calculator和result_printer调用,仅在不需要进入day_calculator的场景(am_pm <1)单独调用相关逻辑。

3. 修复result_printer的提前返回问题

把循环里的return改成格式化变量,避免提前退出函数:

def result_printer(new_hour, new_minute, new_am_pm, day_count, weekday):
    formatted_hour = f"{new_hour:02d}"
    formatted_minute = f"{new_minute:02d}"
    
    day_suffix = ""
    if day_count != 0:
        day_suffix = "(next day)" if day_count ==1 else f"({day_count} days later)"
    
    print(f"{formatted_hour}:{formatted_minute} {new_am_pm}, {weekday.capitalize()} {day_suffix}")

4. 简化天数与AM/PM计算逻辑

去掉冗余的timeday_am和timeday_pm列表,直接用am_pm值计算:

def day_calculator(new_hour, new_minute, new_am_pm, am_pm, weekday, day_count):
    day_count = am_pm // 2  # 每24小时(两次AM/PM切换)算一天
    if am_pm %2 ==1:  # 奇数次切换则反转AM/PM
        new_am_pm = "AM" if new_am_pm == "PM" else "PM"
    
    print(f"This is the new time of day: {new_am_pm}")
    print(f"This is the day count: {day_count}")

    if weekday is not None:
        print(weekday)
        print("Let's calculate the weekday")
        weekday_calculator(weekday, day_count, new_hour, new_minute, new_am_pm)

5. 修复adjust_weekday的参数问题

让adjust_weekday返回计算结果,并传递正确的参数:

def adjust_weekday(define_weekday):
    adjusted_index = define_weekday % len(day_names)
    adjusted_weekday = day_names[adjusted_index]
    print(f"This is the new weekday {adjusted_weekday}")
    return adjusted_weekday

# 在weekday_calculator中调用
elif weekday_calculate >6:
    print("let's adjust the weekday")
    adjusted_weekday = adjust_weekday(weekday_calculate)
    result_printer(new_hour, new_minute, new_am_pm, day_count, adjusted_weekday)

完整修复代码

day_names = [
    "monday",
    "tuesday",
    "wednesday",
    "thursday",
    "friday",
    "saturday",
    "sunday",
]

def result_printer(new_hour, new_minute, new_am_pm, day_count, weekday):
    formatted_hour = f"{new_hour:02d}"
    formatted_minute = f"{new_minute:02d}"
    
    day_suffix = ""
    if day_count != 0:
        day_suffix = "(next day)" if day_count == 1 else f"({day_count} days later)"
    
    print(f"{formatted_hour}:{formatted_minute} {new_am_pm}, {weekday.capitalize()} {day_suffix}")


def adjust_weekday(define_weekday):
    adjusted_index = define_weekday % len(day_names)
    adjusted_weekday = day_names[adjusted_index]
    print(f"This is the new weekday {adjusted_weekday}")
    return adjusted_weekday


def weekday_calculator(weekday, day_count, new_hour, new_minute, new_am_pm):
    print(f"starting weekday:{weekday}")
    weekday = weekday.lower()
    starting_day_index = day_names.index(weekday)
    print(f"This is the starting day of the week's index: {starting_day_index}")
    print(f"This is the day count {day_count}")

    weekday_calculate = starting_day_index + day_count

    if weekday_calculate <= 6:
        new_weekday = day_names[weekday_calculate]
        print(f"This is the new weekday {new_weekday}")
        result_printer(new_hour, new_minute, new_am_pm, day_count, new_weekday)

    elif weekday_calculate > 6:
        print("let's adjust the weekday")
        adjusted_weekday = adjust_weekday(weekday_calculate)
        result_printer(new_hour, new_minute, new_am_pm, day_count, adjusted_weekday)


def day_calculator(new_hour, new_minute, new_am_pm, am_pm, weekday, day_count):
    day_count = am_pm // 2
    if am_pm % 2 == 1:
        new_am_pm = "AM" if new_am_pm == "PM" else "PM"
    
    print(f"This is the new time of day: {new_am_pm}")
    print(f"This is the day count: {day_count}")

    if weekday is not None:
        print(weekday)
        print("Let's calculate the weekday")
        weekday_calculator(weekday, day_count, new_hour, new_minute, new_am_pm)


def time_calculator(init_time: str, add_time: str, weekday: str):
    day_count = 0

    new_am_pm = init_time.split(" ")[1]

    init_hour = int(init_time.split(":")[0])
    init_minute_part = init_time.split(":")[1]
    init_minute = int(init_minute_part.split(" ")[0])

    add_hour = int(add_time.split(":")[0])
    add_minute = int(add_time.split(":")[1])

    print(f"1. Initial hour: {init_hour}, initial minute: {init_minute}")

    new_minute = init_minute + add_minute
    new_hour = init_hour + add_hour

    if new_minute >= 60:
        new_minute -= 60
        new_hour += 1

    am_pm = new_hour // 12
    print(f"AM/PM coefficient: {am_pm}")

    # 处理12小时制的特殊情况(比如0点转12点)
    if new_hour > 12:
        new_hour = new_hour - (am_pm * 12)
    elif new_hour == 0:
        new_hour = 12

    print(f"New hour: {new_hour}, new minute: {new_minute}")

    if am_pm < 1:
        if weekday is not None:
            weekday_calculator(weekday, day_count, new_hour, new_minute, new_am_pm)
        result_printer(new_hour, new_minute, new_am_pm, day_count, weekday)
    else:
        day_calculator(new_hour, new_minute, new_am_pm, am_pm, weekday, day_count)


time_calculator("3:10 PM", "23:20", "tuesday")

内容的提问来源于stack exchange,提问作者Jess

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 06:45:37