簡體   English   中英

打破列表的多個輸入的While循環......用於嵌套列表

[英]Breaking While Loop for multiple inputs of list…for nested list

您好我正在嘗試以列表的形式獲取多個輸入,該列表嵌套在列表中。 我能夠得到 output 但無法脫離循環。

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']]

這是我正在尋找的 output 的類型,但是每當我按 0 時……它都不會中斷循環……

還使用 if val == list(0) 和 if list(val) == list(0) 以及許多其他排列要么給我一個錯誤,要么不從 While 循環中跳出...請幫助..謝謝

input的返回值是str的一個實例。 因此,無論如何,您都需要在 if 語句中使用條件val == '0' 但是由於您是從input的返回值創建一個列表,因此您需要將代碼更改為以下內容:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM