三天的小张 2021-09-11 19:10 采纳率: 100%
浏览 1483
已结题

做练习时有警告:name can be undefined,如何解决?

最后打印时date提示name 'date' can be undefined,该怎么解决?


# 题目:输入某年某月某日,判断这一天是这一年的第几天?

def is_leap(yr):
    if yr % 400 == 0 or ((yr % 400 != 0) and (yr % 4 == 0)):
        return True


while True:
    year = int(input('输入年份:'))
    month = int(input('输入月份:'))
    if month not in range(1, 13):
        print('输入的月份不对,请重新输入。')
        continue
    else:
        date = int(input('输入日期:'))
    if month == 2:
        if is_leap(year):
            if date not in range(1, 30):
                print('输入的日期不对,请重新输入。')
                continue
        else:
            if date not in range(1, 29):
                print('输入的日期不对,请重新输入。')
                continue
    else:
        if month in (1, 3, 5, 7, 8, 10, 12):
            if date not in range(1, 32):
                print('输入的日期不对,请重新输入。')
                continue
        else:
            if date not in range(1, 31):
                print('输入的日期不对,请重新输入。')
                continue
    break
lst = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 12]
count = 0
if month == 1:
    print('%d年1月%d日是%d年的第%d天' % (year, date, year, date))
elif month == 2:
    print('%d年2月%d日是%d年的第%d天' %(year, date, year, 31 + date))
else:
    for i in range(1, month):
        count = count + lst[i]
    if is_leap(year):
        print('%d年%d月%d日是%d年的第%d天' %(year, month, date, year, count + date + 1))
    else:
        print('%d年%d月%d日是%d年的第%d天' % (year, month, date, year, count + date))

  • 写回答

3条回答 默认 最新

  • Pliosauroidea 2021-09-11 19:14
    关注

    就是说编译器检测你的date有可能是不会被定义的,如果实际上不存在这种情况的话可以直接忽略,或者在开头部分定义一个date=0即可

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月19日
  • 已采纳回答 9月11日
  • 创建了问题 9月11日

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号