Niksan555 Niksan555 - 1 month ago 10
Python Question

Class Bug - NoneType and Int - Python

I have a coursework to do. I need to pass all the 6 tests that are required. I've passed 5, but having problem with one regarding the classes in 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)


I cannot pass this test:

type(doomsday(2010))
class 'int'


And the error is:

Failed example:
type(doomsday(2010))
Expected:
class 'int'
Got:
0
class 'NoneType'

Answer

Your bug is probably coming from the fact that you think print() returns some value. It does not.

Your function does not return anything, therefore an implicit return with a return value of None will take place. If you want your function to return an integer, you need to do it explicitly. i.e Replace your print()'s with returns

From the 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. [...]

(emphasis mine)

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

    etc...
Comments