Discuss / Python / 作业

作业

Topic source

LesLieM樂

#1 Created at ... [Delete] [Delete and Lock User]

一定要严格按Python交互式环境中的输入输出来写, 少一个空格都会出错

#!\usr\bin\env\python3
#-*- coding: utf-8 -*-

def fact(n):
    '''
    Example:

    >>> fact(1)
    1
    >>> fact(5)
    120
    >>> fact(-1)
    Traceback (most recent call last):
      ...
    ValueError: n must be greater than 1 or equal to 1
    >>> fact('a')
    Traceback (most recent call last):
      ...
    TypeError: unorderable types: str() < int()

    '''
    if n < 1:
        raise ValueError('n must be greater than 1 or equal to 1')
    if n == 1:
        return 1
    return n * fact(n - 1)

if __name__ == '__main__':
    from doctest import testmod
    testmod()

  • 1

Reply