嘿伙计们,我对这个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()
答案 0 :(得分:1)
这一行肯定存在一个问题:
year = eval(input('Please enter a year: '))
您为什么使用eval
?只需使用year = int(input('Please enter a year: '))
即可。如果您正确输入年份,您的代码将起作用(我对其进行了测试)。
答案 1 :(得分:1)
如果您允许异常传播,那么您将获得回溯:
# 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
阻止,如果需要,请随时重新添加。
答案 2 :(得分:0)
问题是你必须为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
答案 3 :(得分:0)
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)
答案 4 :(得分:-2)
您的代码问题是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()