基本上,当我们输入
之类的列表时[['Bilbo', 'Baggins'], ['Gollum'], ['Tom', 'Bombadil'], ['Aragorn']]
在函数中,它应该在单独的行中打印出每个列表中的元素,并在它们之间留一个空格。当每个列表中的元素数不相等时,我不知道该怎么办。
我也不允许使用任何for循环
这是我的代码:
def print_names2(people):
"""Print a list of people's names, which each person's name
is itself a list of names (first name, second name etc)
"""
i = 0
while i < len(people):
names = list(people[i])
j = 0
while j < len(names):
i += 1
name = names[j]
print(name, end=" ")
j += 1
print_names2([['Bilbo', 'Baggins'], ['Gollum'], ['Tom', 'Bombadil'],
['Aragorn']])
预期结果:
Bilbo Baggins
Gollum
Tom Bombadil
Aragorn
实际结果:
Bilbo Baggins Tom Bombadil
我应该对我的代码进行哪些更改?
谢谢。
答案 0 :(得分:2)
尝试一种更简单的方法。
router.post(
"/",
[
check("name", "Name is required")
.not()
.isEmpty(),
check("email", "Please include valid email").isEmail(),
check(
"password",
"Please enter password with 5 or more characters"
).isLength({ min: 5 })
],
(req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
res.send("User Route");
}
);
输出
def print_names(people):
for i in people:
print (' '.join(i))
people=[['Bilbo', 'Baggins'], ['Gollum'], ['Tom', 'Bombadil'], ['Aragorn']]
print_names(people)
不使用Bilbo Baggins
Gollum
Tom Bombadil
Aragorn
循环。
for
答案 1 :(得分:1)
您只需要在初始while循环的末尾打印一个空行。
def print_names2(people):
"""Print a list of people's names, which each person's name
is itself a list of names (first name, second name etc)
"""
i = 0
while i < len(people):
names = list(people[i])
j = 0
while j < len(names):
i += 1
name = names[j]
print(name, end=" ")
j += 1
print("")
print_names2([['Bilbo', 'Baggins'], ['Gollum'], ['Tom', 'Bombadil'],
['Aragorn']])