没有定义变量'dateNumber'的Python程序

时间:2015-07-22 05:40:14

标签: python

def dateCalculationNorm(year):
    a = year%19
    b = year%4
    c = year%7
    d = (19*a + 24)%30
    e = (2*b + 4*c + 6*d + 5)%7
    dateNumber = 22 + d + e
    return dateNumber

def dateCalculationSpecial(year):
    a = year%19
    b = year%4
    c = year%7
    d = (19*a + 24)%30
    e = (2*b + 4*c + 6*d + 5)%7
    dateNumber = 15 + d + e
    return dateNumber

def dateOutput(year, date):
    print("The date of Easter in the year {0} is {1}.".format(year, date))

def main():
    print("Easter Date Calculator")
    print()
    year = eval(input("Enter the year: "))

    if year >= 1900 and year <= 2099:
        dateCalculationNorm(year)
        if dateNumber > 31:
            date = "April " + str(dateNumber - 31)
            dateOutput(year, date)
        else:
            date = "March " + str(dateNumber)
            dateOutput(year, date)
    elif year == 1954 or year == 1981 or year == 2049 or year == 2076:
        dateCalculationSpecial(year)
        if dateNumber > 31:
        date = "April " + str(dateNumber - 31)
        dateOutput(year, date)
        else:
            date = "March " + str(dateNumber)
            dateOutput(year, date)
    else:
    print("Sorry, but that year is not in the range of this program.")

if __name__ == '__main__':
    main()

我无法让main()接受以下行中的dateNumber (如果year> = 1900且year&lt; = 2099)Python说没有定义dateNumber。我尝试在程序开始时使dateNumber全局化并且有效(尽管有来自Python的警告),但我知道这是让这个程序运行的草率方式。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您需要实例化dateNumber

if year >= 1900 and year <= 2099:
    dateNumber = dateCalculationNorm(year) # Instantiate here.
    if dateNumber > 31:
        date = "April " + str(dateNumber - 31)
        dateOutput(year, date)
    else:
        date = "March " + str(dateNumber)
        dateOutput(year, date)

目前,main()功能中没有任何内容。也没有必要将它设置为全局。