列表项
# To put values in list till user want
#comparing value entered with the ascii value ofenter
# key , if enter then come out of infinite loop
# why break is not breaking out on enter press
#if value is not enter_key thn put this value in list
x=1
lis=[]
while x == 1 :
var = str(input())
if var == chr(10):
break
lis.append(var)
print("i m free now from infinite loop")
print(lis)
答案 0 :(得分:2)
如果在str(input())
提示下用户按下Enter键而不输入任何内容,则返回值将为空字符串。因此,您不应该将var
与chr(10)
(换行符(\n
)进行比较。而是尝试以下方法:
x=1
lis=[]
while x == 1 :
var = str(input())
if var == "": #Compare to an empty string!
break
lis.append(var)
print("i m free now from infinite loop")
print(lis)
答案 1 :(得分:1)
我认为用户想要的是停在一个空字符串上。所以我将代码编写如下
a_list=[]
while True :
var = input('What is your input: ')
if not var:
break
a_list.append(var)
print("I'm free now from the infinite loop")
print(a_list)