# write whole function definition for make_numberlist including the
# function header and body so the doctests pass
"""
Return a list of the numbers from first to last exclusive with an
optional step.
>>> make_numberlist(3,9,2)
[3, 5, 7]
>>> make_numberlist(-3,2)
[-3, -2, -1, 0, 1]
>>> make_numberlist(5,1,-1)
[5, 4, 3, 2]
"""
def reverse_section(alist, start, end):
"""
Reverse the order of the items in alist between start and end inclusive
Return a new list leaving alist unchanged.
>>> testlist = [1,2,3,4,5,6,7]
>>> reverse_section(testlist, 1, 3)
[1, 4, 3, 2, 5, 6, 7]
>>> testlist
[1, 2, 3, 4, 5, 6, 7]
>>> reverse_section(["bob","sue","mary","jim","lucy"], 3,4)
['bob', 'sue', 'mary', 'lucy', 'jim']
"""
def print_function_table():
"""
Prints a table of values for i to the power of 1,2,5 and 10 from 1 to 10
left aligned in columns of width 4,5,8 and 13
>>> print_function_table()
i i**2 i**5 i**10
1 1 1 1
2 4 32 1024
3 9 243 59049
4 16 1024 1048576
5 25 3125 9765625
6 36 7776 60466176
7 49 16807 282475249
8 64 32768 1073741824
9 81 59049 3486784401
10 100 100000 10000000000
"""
def count_words_in_file(filename):
"""
Return the number of whitespace separated words in the given file
>>> count_words_in_file("mary.txt")
39
"""
def sum_numbers_in_file(filename):
"""
Return the sum of the numbers in the given file (which only contains
integers separated by whitespace).
>>> sum_numbers_in_file("numbers.txt")
19138
"""
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
对于第二个函数, 不要出了range的范围。