【发布时间】:2021-06-30 18:41:21
【问题描述】:
您好,我正在尝试以列表的形式获取多个输入,该列表嵌套在列表中。我能够得到输出,但无法跳出循环。
values = []
while True:
val = list(input("Enter an integer, the value ends if it is 'stop': "))
if val == 0:
exit()
values.append(val)
print(values)
我什至在 if 条件之后使用了 break..no Joy !!
Enter an integer, the value ends if it is 'stop': 4569
[['4', '5', '6', '9']]
Enter an integer, the value ends if it is 'stop': 666655
[['4', '5', '6', '9'], ['6', '6', '6', '6', '5', '5']]
Enter an integer, the value ends if it is 'stop': 5222
[['4', '5', '6', '9'], ['6', '6', '6', '6', '5', '5'], ['5', '2', '2', '2']]
Enter an integer, the value ends if it is 'stop': 0
[['4', '5', '6', '9'], ['6', '6', '6', '6', '5', '5'], ['5', '2', '2', '2'], ['0']]
这是我正在寻找的输出类型,但每当我按 0...它不会中断循环..
也使用 if val == list(0) 和 if list(val) == list(0) 和许多其他排列要么给我一个错误,要么不会从 While 循环中中断...请帮助..谢谢
【问题讨论】:
-
在您的示例输出中,您不会在任何地方输入
'stop'。这是一个错误吗?从输入消息看来,'stop'应该结束当前子列表并开始下一个。请说明输入“停止”和 0 应该做什么。 -
其实我得到了解决方案...无法删除它...我的错误是如果 val == (['0'])
-
好吧,我打算使用 'stop' 或 '0'...不管它必须是列表中的 str 值
标签: python list loops input while-loop