i =0
A=[[],[],[]]
while i < 3:
j=0
n=int(input("n: "))
while j<n:
A[i].append((input("> ")))
j+=1
i+=1
print(A)
这样可以得到:
^{pr2}$
另外,当你知道你的循环要迭代多少次,而不是使用while你可以做for,类似这样:
^{3}$
同样的结果;)
编辑:
根据@kabanus的假设,如果您真的想要这种输入,则需要拆分给定的字符串:
A = []
for i in range(3):
n = int(input("n: "))
while True:
words = input("> ").split()
if len(words) == n:
break
print(f"You gave {len(words)} words, you must give {n} words! Try again.")
A.append(words)
print(A)
这样可以得到:
n: 4
> n hgh hjhgj jhh
n: 1
> jghj
n: 3
> a b c
[['n', 'hgh', 'hjhgj', 'jhh'], ['jghj'], ['a', 'b', 'c']]
您的输入不正确,您的代码要求您在不同的输入中提供字符串,而不是在一行中。为了确保输入询问的是什么,您可以给
input
一个文本,以便它在控制台中显示:这样可以得到:
^{pr2}$另外,当你知道你的循环要迭代多少次,而不是使用
^{3}$while
你可以做for
,类似这样:同样的结果;)
编辑:
根据@kabanus的假设,如果您真的想要这种输入,则需要拆分给定的字符串:
这样可以得到:
添加了一个
while
循环以继续询问,如果给出的单词数不正确,则会显示一条小消息。在相关问题 更多 >
编程相关推荐