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

Python日期魔法判断程序报错求助:月日乘积匹配年份逻辑问题

Python魔法日期判断代码修复

原代码存在的问题:

  • 输入提示文本不符合需求,未按要求显示Enter month (numeric):这类规范提示
  • input()获取的内容是字符串类型,直接相乘会变成字符串拼接,无法完成数值运算
  • 条件判断误用赋值符号=,应使用比较符号==
  • else语句缩进错误,未和对应if保持同一层级

修正后的代码:

# 按要求获取输入并转换为整数类型
month = int(input("Enter month (numeric): "))
day = int(input("Enter day: "))
year = int(input("Enter two digit year: "))

# 数值比较并输出结果
if month * day == year:
    print("This date is magic!")
else:
    print("This date is not magic")

运行示例:

输入:
Enter month (numeric): 12
Enter day: 8
Enter two digit year: 96
输出:
This date is magic!

输入:
Enter month (numeric): 10
Enter day: 2
Enter two digit year: 75
输出:
This date is not magic

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 11:25:30