我有功课要做。我需要通过所有6项考试。我已经过了5级,但是对于Python中的类有一个问题。你知道吗
def doomsday(y):
"""
>>> doomsday(2012)
3
>>> doomsday(1899)
2
>>> doomsday(1923)
3
>>> doomsday(10000)
-1
>>> doomsday(1756)
-1
>>> type(doomsday(2010))
<class 'int'>
"""
try:
y
except ValueError:
return
if y in range (1800, 1899+1):
x = 5
w = y%100
a = w//12
b = w%12
c = b//4
d = (a + b + c)%7
t = x + d
if t>6:
t = t - 7
print (t)
else:
print (t)
elif y in range (1900, 1999+1):
x = 3
w = y%100
a = w//12
b = w%12
c = b//4
d = (a + b + c)%7
t = x + d
if t>6:
t = t - 7
print (t)
else:
print (t)
elif y in range (2000, 2099+1):
x = 2
w = y%100
a = w//12
b = w%12
c = b//4
d = (a + b + c)%7
t = x + d
if t>6:
t = t - 7
print (t)
else:
print (t)
elif y in range (2100, 2199+1):
x = 0
w = y%100
a = w//12
b = w%12
c = b//4
d = (a + b + c)%7
t = x + d
if t>6:
t = t - 7
print (t)
else:
print (t)
else:
x = -1
print (x)
我不能通过这个测试:
type(doomsday(2010))
class 'int'
错误是:
Failed example:
type(doomsday(2010))
Expected:
class 'int'
Got:
0
class 'NoneType'
您的bug可能来自这样一个事实:您认为
print()
返回了一些值。它没有。你知道吗函数不返回任何内容,因此将发生返回值为None的隐式返回。如果希望函数返回整数,则需要显式执行。i、 用
return
替换你的print()
从Python Documentation on functions:
(我的重点)
def世界末日(y):
只需要返回值而不是打印它们:
相关问题 更多 >
编程相关推荐