Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么Break不是这段代码的循环中断?_Python_Python 3.x_Infinite Loop_Break - Fatal编程技术网

Python 为什么Break不是这段代码的循环中断?

Python 为什么Break不是这段代码的循环中断?,python,python-3.x,infinite-loop,break,Python,Python 3.x,Infinite Loop,Break,列表项 # 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

列表项

# 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)

我认为用户想要的是在一个空字符串上停止。因此,我将制定如下代码

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())
提示时按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)

如果用户在出现
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)

因为如果用户只是点击enter,结果是一个空字符串,而不是换行符。此外,输入的结果已经是一个字符串。为什么要与
chr(10)
进行比较???当
input
提示时,您是否查看了通过简单按enter键返回的内容?
str(input())
中的
str
是多余的
input()
返回一个字符串。因为如果用户只点击enter,结果是一个空字符串,而不是换行符。此外,输入的结果已经是一个字符串。为什么要与
chr(10)
进行比较???当
input
提示时,您是否查看了通过简单按enter键返回的内容?
str(input())
中的
str
是多余的
input()
返回字符串。input已经返回字符串,不需要
str(input(..)
如果不是var:break
将起到与空字符串错误相同的作用-请参阅input已经返回字符串,不需要
str(input(..))
如果不是var:break,则使用与空字符串错误相同的用途-请参阅