Reputation: 13
List item
# 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)
Upvotes: 0
Views: 75
Reputation: 3465
I think what the user wants is to stop on an empty string. So I would make the code as follows
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)
Upvotes: 1
Reputation: 153
If the user presses the enter key without entering anything when prompted by str(input())
, then the return value will be an empty string. Thus, you shouldn't be comparing var
to chr(10)
, which is the newline character (\n
). Instead try this:
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)
Upvotes: 2