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

Zeller同余算法项目变量命名及代码错误排查求助

Zeller同余算法星期计算程序错误修复求助

我正在学习Python数组相关章节,编写了一个基于Zeller同余算法的程序,功能是让用户输入月、日、年,计算对应的星期几。目前程序接近完成,但有两行代码出错,尝试通过修改变量命名(切换大小写、改用蛇形命名)后反而出现更多错误。以下是我的代码:

def calc_zellers_congruence(get_day: int, get_year: int, get_month: int) -> str:
    # Calculates the month day and year
    days = [""] * 7

    day_Of_Week = 0
    day_of_month = 0
    century = 0
    year_of_the_century = get_year % 100
    days[1] = "Sunday"
    days[2] = "Monday"
    days[3] = "Tuesday"
    days[4] = "Wednessday"
    days[5] = "Thursday"
    days[6] = "Friday"
    days[0] = "Saturday"
    dayOfWeek = day_of_month + 13 * get_month + int(1) / 5 + year_of_the_century + int(year_of_the_century) / 4 + int(
        century) / 4 + 5 * century % 7
    name_of_day = days[day_Of_Week]

    return name_of_day

def displayResults():
    print("Result" + day)

def get_day():
    print("Enter a Day")
    get_day = int(input())

    return get_day

def get_month():
    print("Enter a Month")
    month = int(input())

    return month

def get_year():
    print("Enter a Year")
    get_year: int(input())

    return year

# Main
# This program will calculate birthdays using arrays and zellers congruence
# References...Programming Fundamentals WiKi, J Robinson, GeeksForGeek.com
year = get_year()
month = get_month()
day = get_day()
calc_Zeller_Congruence: calc_zellers_congruence(get_year, get_month, get_day)
displayResults(calc_zellers_congruence)

修复后的代码

def calc_zellers_congruence(day: int, year: int, month: int) -> str:
    # Zeller同余算法处理:1、2月视为上一年的13、14月
    if month < 3:
        month += 12
        year -= 1
    
    days = [
        "Saturday", 
        "Sunday", 
        "Monday", 
        "Tuesday", 
        "Wednesday", 
        "Thursday", 
        "Friday"
    ]
    
    century = year // 100
    year_of_century = year % 100
    
    # 正确的Zeller公式计算(使用整数除法)
    day_of_week = (
        day 
        + (13 * (month + 1)) // 5 
        + year_of_century 
        + year_of_century // 4 
        + century // 4 
        + 5 * century
    ) % 7
    
    return days[day_of_week]

def display_results(result: str):
    print(f"Result: {result}")

def get_day() -> int:
    print("输入日期:")
    return int(input())

def get_month() -> int:
    print("输入月份:")
    return int(input())

def get_year() -> int:
    print("输入年份:")
    return int(input())

# 主程序
if __name__ == "__main__":
    year = get_year()
    month = get_month()
    day = get_day()
    # 调用函数时传入正确的变量,而非函数名
    week_day = calc_zellers_congruence(day, year, month)
    display_results(week_day)

主要修复点

  • get_year函数语法错误:原代码中get_year: int(input())写法错误,改为正确的变量赋值与返回逻辑
  • Zeller公式核心错误:
    补充1、2月转成上一年13、14月的特殊规则
    把浮点数除法改为整数除法,避免计算结果出现小数
    正确计算世纪数century,修正公式运算顺序与符号
  • 变量与调用错误:
    统一变量命名,避免day_Of_Week和dayOfWeek这类同名不同大小写的混淆
    调用计算函数时传入已获取的变量值,而非函数名,同时修正赋值符号(冒号改等号)
  • 函数参数问题:给displayResults添加参数接收计算结果,改为格式化输出
  • 拼写修正:把Wednessday改为正确的Wednesday
  • 规范优化:统一使用蛇形命名法,符合Python编码规范

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 06:10:36