嘿伙计们,我在这个 python 问题上遇到了另一个问题。
计算
years 1982 – 2048
(含)中的复活节的公式如下:
让
a = year %19
b = year %4
c = year % 7
d = (19a + 24)%30
e= (2b + 4c + 6d + 5) % 7
复活节的日期是
March 22 + d + e
(可能是四月)。编写一个程序,输入年份,验证它是否在正确的范围内,然后打印出当年的复活节日期。此外,编写一个函数 easterii() 来执行此操作。该函数不接受任何参数并返回一个值。使用 return 语句。
这是我目前拥有的代码。当我输入日期时,它总是输出
"There was a problem, try again."
如有任何意见,我们将不胜感激,谢谢。
def easterCal(year):
a = year % 19
b = year % 4
c = year % 7
d = ((19 * a) + 24) % 30
e = ((2 * b) + (4 * c) + (6 * d) + 5) % 7
date = 22+d+e
return date
def easterii():
try:
year = eval(input('Please enter a year: '))
year = int(year)
if year < 1982:
print("Year is out of range")
elif year > 2048:
print("Year out of range")
else:
date = easterCal(year)
if 22 <= date <= 31:
print('Easter date for year {0} is March,{1}'.format(year , date))
elif 32 <= date <= 56:
print('Easter date for year {0} is April,{1}'.format(year,date-31))
else:
print('Incorrect.')
except NameError:
print('Please enter date in numbers')
except:
print('There was a problem, try again.')
if __name__ == '__main__':
easterii()
有一个问题肯定是在这一行:
year = eval(input('Please enter a year: '))
你为什么使用
eval
?只需使用year = int(input('Please enter a year: '))
。如果您输入的年份正确,您的代码将有效(我测试过)。
如果您让异常传播,您将收到以下回溯:
# input year out of range, e.g. 1952
Traceback (most recent call last):
File "...\test.py", line 32, in <module>
main()
File "...\test.py", line 23, in main
if 22 <= date <= 31:
UnboundLocalError: local variable 'date' referenced before assignment
原因是,在您的代码中,如果年份不正确,您不会计算
date
,但仍然在 if
子句中引用它。
main
的正确代码是:
def main():
year = int(input('Please enter a year: '))
if 1982 < year < 2048:
date = easterCal(year)
if 22 <= date <= 31:
print('Easter date for year {0} is March,{1}'.format(year , date))
elif 32 <= date <= 56:
print('Easter date for year {0} is April,{1}'.format(year, date-31))
else:
print("Year is out of range")
请注意,我已删除
try-catch
块,如果需要,请随时将其重新添加回来。
问题是您必须向 eval() 提供一个字符串,如代码引发的异常所述:
eval() arg 1 must be a string or code object
在这一行
year = eval(input('Please enter a year: '))
你可以
str(eval(input()))
请注意,您可以通过以下方式打印异常
except Exception, e:
prtint e
def dateEaster(year):
if year >= 1900 and year <= 2099:
a = year % 19
b = year % 4
c = year % 7
d = (19 * a + 24) % 30
e = (2 * b + 4 * c + 6 * d +5) % 7
dateofeaster = 22 + d + e
if year == 1954 or year == 1981 or year == 2049 or year == 2076:
dateofeaster = dateofeaster - 7
if dateofeaster > 31:
dateofeaster = dateofeaster - 31
print("April", dateofeaster)
else:
print("March", dateofeaster)
else:
print("There is an error")
year = int(input("Enter a year"))
dateEaster(year)
我见过的所有有关复活节日期计算的程序输出的结果都是 1986 年的 3 月 30 日。但是正确的日期是 4 月 13 日。这是因为,当复活节日期为 3 月 22+d+e 时,如果年份是 1986 年,则天数为 44,与 4 月有 13 天重叠。那你们怎么过3月30日呢?
您的代码的问题是
eval
。您的代码中几乎没有冗余,例如在赋值之前引用了局部变量date
。因此,明智的做法是将 date
声明为 global
,并且您可以将 if
和 elif
组合在一起。
def easterCal(year):
a = year % 19
b = year % 4
c = year % 7
d = ((19 * a) + 24) % 30
e = ((2 * b) + (4 * c) + (6 * d) + 5) % 7
date = 22 + d + e
return date
def main():
global date
try:
year = int(input('Please enter a year: '))
if 1982 < year > 2048:
print("Year is out of range")
else:
date = easterCal(year)
if 22 <= date <= 31:
print('Easter date for year {0} is March,{1}'.format(year, date))
elif 32 <= date <= 56:
print('Easter date for year {0} is April,{1}'.format(year, date - 31))
else:
print('Incorrect.')
except NameError:
print('Please enter date in numbers')
except:
print('There was a problem, try again.')
if __name__ == '__main__':
main()