类别错误 - NoneType 和 Int - Python

2024-10-03 09:11:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我有功课要做。我需要通过所有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'

Tags: inifdeftyperangeelseclassint
2条回答

您的bug可能来自这样一个事实:您认为print()返回了一些值。它没有。你知道吗

函数不返回任何内容,因此将发生返回值为None的隐式返回。如果希望函数返回整数,则需要显式执行。i、 用return替换你的print()

Python Documentation on functions

[...] even functions without a return statement do return a value [...], This value is called None (it’s a built-in name). Writing the value None is normally suppressed by the interpreter if it would be the only value written. [...]

(我的重点)

def世界末日(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...

只需要返回值而不是打印它们:

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

相关问题 更多 >