[英]Ensure multiple user inputs to list are integers within a while loop Python 3
[英]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.