user2425814
user2425814

Reputation: 409

Python 3 booleans

So, for below I got: return century == year // 100 + 1 or century == year / 100

However, I'm not satisfying the last one:

>>>in_century(2013, 20)
False

How do I make it so that it is only True if the century is exactly equal to year divided by 100? Also, is the format of the expression more or less correct?

Thank you!

Here is the question:

def in_century(year, century):
    '''(int, int) -> bool

    Return True iff year is in century.

    Remember, for example, that 1900 is the last year of the 19th century,
    not the beginning of the 20th.

    year will be at least 1.

    >>> in_century(1994, 20)
    True
    >>> in_century(1900, 19)
    True
    >>> in_century(2013, 20)
    False
    '''

Upvotes: 0

Views: 137

Answers (1)

Dietrich Epp
Dietrich Epp

Reputation: 213837

So, your code is this?

def in_century(year, century):
    return century == year // 100 + 1 or century == year / 100

You probably don't want an or here.

>>> in_century(2000, 20)
True
>>> in_century(2000, 21)
True

Try calculating the century for a year directly, and then comparing.

def century_from_year(year):
    return (year - 1) // 100 + 1

def in_century(year, century):
    return century_from_year(year) == century

Upvotes: 1

Related Questions