python 问题

# 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的范围。

程序是没有问题的,我在解释器下运行也是正常的。在解释器输入的时候要注意缩进,并且一定要注意不要多个语句块一起输入

至于ans = ops[op](*nums)
ops = 是个字典
op则等于+或者-, 假设op是'+',则ops[op] 则取出add这个函数
而后面的(*nums)则相当于将nums中的元素一次作为参数传递给add这个函数,比如nums = [3,4]
则ops[op](*nums) 相当于 add(3,4)

至于这样的调用函数的形式,你可以去看下python2.x的内置函数apply,这里的ops[op]相当于apply的functions函数,*nums相当于apply的args函数

apply不存在于3.x版本中
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-12
import re

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']
"""
rtn = alist[:]
part = alist[start:end+1]
rtn[start:end+1] = part[::-1]
return rtn

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
"""
print('%-4s%-5s%-8s%-13s'%('i','i**2','i**5','i**10'))
fmt = '%-4d%-5d%-8d%-13d'
def _ln(i):
print(fmt % (i, pow(i,2), pow(i,5), pow(i,10)))
map(_ln,range(1,11))

def count_words_in_file(filename):
"""
Return the number of whitespace separated words in the given file
>>> count_words_in_file("mary.txt")
39
"""
patt = re.compile('\w')
return len(patt.findall(open(filename,'rt').read()))

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
"""
patt = re.compile(r'\d+')
return sum(patt.findall(open(filename,'rt').read()))

if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
相似回答