为什么 Break 不是 Breaking out of loop 对于这一小段代码?
why Break is not Breaking out of loop for this small piece of code?
列表项
# 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)
我认为用户想要的是停在一个空字符串上。所以我会按如下方式编写代码
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)
如果用户在 str(input())
提示时按下回车键而没有输入任何内容,那么 return 值将是一个空字符串。因此,您不应将 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)
列表项
# 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)
我认为用户想要的是停在一个空字符串上。所以我会按如下方式编写代码
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)
如果用户在 str(input())
提示时按下回车键而没有输入任何内容,那么 return 值将是一个空字符串。因此,您不应将 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)