2016-10-27 183 views
0

我有一个课程要做。我需要通过所有需要的6个测试。我已经通过了5次,但有一个关于Python中的类的问题。类错误 - 无类型和诠释 - 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' 
+1

你的函数不返回任何东西,所以用的'返回值的隐含回报没有'会发生。如果你想让你的函数返回一个整数,你需要明确地做。 –

+1

'print'不是'return' –

回答

0

你错误可能来自于你瘦身的事实k print()返回一些值。它不是。

您的函数不会返回任何内容,因此会返回一个返回值为None的隐式返回值。如果你想让你的函数返回一个整数,你需要明确地做。没有return语句

[...]偶函数都返回一个值[...],这个值被称为无:即与return小号

Python Documentation on functions替换您print()的(这是一个内置的名字)。写入值None通常会被解释器抑制,如果它是唯一写入的值。 [...]

(重点煤矿)

高清世界末日(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 
     return t 
    else: 
     return t 

    etc... 
0

刚需,而不是返回打印它们的值:

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 
      return t 
     else: 
      return 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 
      return t 
     else: 
      return 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 
      return t 
     else: 
      return 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 
      return t 
     else: 
      return t 

    else: 
     x = -1 
     return x 
相关问题