做练习时有警告: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))

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

换个变量

OK了,之前尝试在循环开头加的,又出来其他警告了,加到循环前面就好了。