2013-05-30 65 views
0

所以,下面我得到:return century == year // 100 + 1 or century == year/100的Python 3布爾

不過,我不滿足最後一個:

>>>in_century(2013, 20) 
False 

如何讓這個是隻有真要是世紀恰好等於一年除以100? 另外,是表達式的格式或多或少正確的嗎?

謝謝!

這裏有一個問題:

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 
    ''' 
+0

(year - 1)// 100 + 1 – cerkiewny

回答

1

所以,你的代碼是什麼?

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

你可能不希望一個or這裏。

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

嘗試直接計算一年的世紀,然後進行比較。

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

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

感謝您的幫助! – user2425814