重复输出问题
def printtable(s):colwidth = * len(s)
for i in range(len(s)):
lst = []
for j in s:
lst.append(len(j))
colwidth = max(lst)
for m in range(len(s)):
for n in range(len(s)):
print(s.rjust(colwidth), end=' ')
print()
tabledata = [['apple', 'orange', 'cherries', 'banana'],
['Alice', 'Bob', 'Carlo', 'David'],
['dog', 'cat', 'moose', 'goose']]
printtable(tabledata)
这个函数应该输出的格式:
apple Alice dog
orange Bob cat
cherries Carlo moose
banana David goose
可以为什么老是重复输出呢? def printtable(s):
colwidth = * len(s)
for i in range(len(s)):
lst = []
for j in s:
lst.append(len(j))
colwidth = max(lst)
for m in range(len(s)): # 缩进
for n in range(len(s)):
print(s.rjust(max(colwidth)), end=' ') # 格式宽度
print()
tabledata = [['apple', 'orange', 'cherries', 'banana'],
['Alice', 'Bob', 'Carlo', 'David'],
['dog', 'cat', 'moose', 'goose']]
printtable(tabledata)
qq1151985918 发表于 2021-6-28 22:44
谢谢,是我题目没看通透,没能好好理解。题目说了假设列表里的列表元素数目都是相同的。而且也提示格式宽度怎么用了。
页:
[1]