封装和泛化函数 def mult_tasks(str_numbers) in python
Encapsulation and generalization function def mult_tasks(str_numbers) in python
问题:
在不改变 mult_tasks 的情况下,为 mult_tasks_line 写一个定义,这样
doctests 通过。
所以:
print mult_tasks("3469")
将产生:
(3*3) (3*4) (3*6) (3*9)
(4*3) (4*4) (4*6) (4*9)
(6*3) (6*4) (6*6) (6*9)
(9*3) (9*4) (9*6) (9*8)
def mult_tasks_line(first_num, str_numbers):
"""
>>> mult_tasks_line("4", "3469")
'(4*3) (4*4) (4*6) (4*9) '
"""
#Add your code here
def mult_tasks(str_numbers):
"""
>>> mult_tasks("246")
'(2*2) (2*4) (2*6) \n(4*2) (4*4) (4*6) \n(6*2) (6*4) (6*6) \n'
>>> mult_tasks("1234")
'(1*1) (1*2) (1*3) (1*4) \n(2*1) (2*2) (2*3) (2*4) \n(3*1) (3*2) (3*3) (3*4) \n(4*1) (4*2) (4*3) (4*4) \n'
"""
#Do not alter any code in this function
result_tasks = ""
for ch in str_numbers:
result_tasks += mult_tasks_line(ch, str_numbers) + "\n"
return result_tasks
if __name__ == "__main__":
import doctest
doctest.testmod(verbose = True)
我已经尝试这样做了:
def mult_tasks_line(first_num, str_numbers):
digits=0
for num in str_numbers:
while digits<len(str_numbers):
print "("+first_num+"*"+str_numbers[digits]+")",
digits=digits+1
def mult_tasks(str_numbers):
result_tasks = ""
for ch in str_numbers:
result_tasks += mult_tasks_line(ch, str_numbers),"\n"
return result_tasks
这是我试过的,第一个函数工作得非常接近,但它没有单引号。当 运行
mult_tasks_line("4", "3469")
结果 (4*3) (4*4) (4*6) (4*9)
。
但是第二个功能似乎完全错误。
这是第二个函数的结果:
mult_tasks("246”)
(2*2) (2*4) (2*6)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module> mult_tasks("246")
File "/Users/HuesFile/Downloads/Mastery Test/2.py", line 25, in mult_tasks
result_tasks += mult_tasks_line(ch, str_numbers),"\n"
TypeError: cannot concatenate 'str' and 'tuple' objects
据我了解,您想从一串数字字符中打印有序元组的矩阵,
而分隔符
def mult_tasks_line(first_num, str_numbers):
return ''.join(
['({n1}*{n2}) '.format(n1 = first_num, n2 = n2 ) for n2 in str_numbers])
def mult_tasks(str_numbers):
result_tasks = ""
for ch in str_numbers:
result_tasks += mult_tasks_line(ch, str_numbers) + "\n"
return result_tasks
然后
print mult_tasks("3469")
给予
(3*3) (3*4) (3*6) (3*9)
(4*3) (4*4) (4*6) (4*9)
(6*3) (6*4) (6*6) (6*9)
(9*3) (9*4) (9*6) (9*9)
随心所欲。从适用于任意字符串的意义上说,结果是可缩放的。
您希望计算乘法吗?
显然 mult_tasks
包含不正确的行
result_tasks += mult_tasks_line(ch, str_numbers),"\n"
result_tasks
是一个字符串,在这里它与一个元组连接(它们由带或不带括号的逗号构成)mult_tasks_line(ch, str_numbers), "\n"
导致异常。
您需要将此行修改为
result_tasks += mult_tasks_line(ch, str_numbers) + "\n"
代码中的另一个问题是 mult_tasks_line
没有 return 值,只是打印出来。你想做这样的事情
def mult_tasks_line(first_num, str_numbers):
result = []
for ch in str_numbers:
result.append('({}*{})'.format(first_num, ch))
return ' '.join(result)
问题:
在不改变 mult_tasks 的情况下,为 mult_tasks_line 写一个定义,这样
doctests 通过。
所以:
print mult_tasks("3469")
将产生:
(3*3) (3*4) (3*6) (3*9)
(4*3) (4*4) (4*6) (4*9)
(6*3) (6*4) (6*6) (6*9)
(9*3) (9*4) (9*6) (9*8)
def mult_tasks_line(first_num, str_numbers):
"""
>>> mult_tasks_line("4", "3469")
'(4*3) (4*4) (4*6) (4*9) '
"""
#Add your code here
def mult_tasks(str_numbers):
"""
>>> mult_tasks("246")
'(2*2) (2*4) (2*6) \n(4*2) (4*4) (4*6) \n(6*2) (6*4) (6*6) \n'
>>> mult_tasks("1234")
'(1*1) (1*2) (1*3) (1*4) \n(2*1) (2*2) (2*3) (2*4) \n(3*1) (3*2) (3*3) (3*4) \n(4*1) (4*2) (4*3) (4*4) \n'
"""
#Do not alter any code in this function
result_tasks = ""
for ch in str_numbers:
result_tasks += mult_tasks_line(ch, str_numbers) + "\n"
return result_tasks
if __name__ == "__main__":
import doctest
doctest.testmod(verbose = True)
我已经尝试这样做了:
def mult_tasks_line(first_num, str_numbers):
digits=0
for num in str_numbers:
while digits<len(str_numbers):
print "("+first_num+"*"+str_numbers[digits]+")",
digits=digits+1
def mult_tasks(str_numbers):
result_tasks = ""
for ch in str_numbers:
result_tasks += mult_tasks_line(ch, str_numbers),"\n"
return result_tasks
这是我试过的,第一个函数工作得非常接近,但它没有单引号。当 运行
mult_tasks_line("4", "3469")
结果 (4*3) (4*4) (4*6) (4*9)
。
但是第二个功能似乎完全错误。 这是第二个函数的结果:
mult_tasks("246”)
(2*2) (2*4) (2*6)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module> mult_tasks("246")
File "/Users/HuesFile/Downloads/Mastery Test/2.py", line 25, in mult_tasks
result_tasks += mult_tasks_line(ch, str_numbers),"\n"
TypeError: cannot concatenate 'str' and 'tuple' objects
据我了解,您想从一串数字字符中打印有序元组的矩阵, 而分隔符
def mult_tasks_line(first_num, str_numbers):
return ''.join(
['({n1}*{n2}) '.format(n1 = first_num, n2 = n2 ) for n2 in str_numbers])
def mult_tasks(str_numbers):
result_tasks = ""
for ch in str_numbers:
result_tasks += mult_tasks_line(ch, str_numbers) + "\n"
return result_tasks
然后
print mult_tasks("3469")
给予
(3*3) (3*4) (3*6) (3*9)
(4*3) (4*4) (4*6) (4*9)
(6*3) (6*4) (6*6) (6*9)
(9*3) (9*4) (9*6) (9*9)
随心所欲。从适用于任意字符串的意义上说,结果是可缩放的。
您希望计算乘法吗?
显然 mult_tasks
包含不正确的行
result_tasks += mult_tasks_line(ch, str_numbers),"\n"
result_tasks
是一个字符串,在这里它与一个元组连接(它们由带或不带括号的逗号构成)mult_tasks_line(ch, str_numbers), "\n"
导致异常。
您需要将此行修改为
result_tasks += mult_tasks_line(ch, str_numbers) + "\n"
代码中的另一个问题是 mult_tasks_line
没有 return 值,只是打印出来。你想做这样的事情
def mult_tasks_line(first_num, str_numbers):
result = []
for ch in str_numbers:
result.append('({}*{})'.format(first_num, ch))
return ' '.join(result)