vedsil
vedsil

Reputation: 137

Stop input when empty line occurs in Python

I hope this is not a duplicate, I checked all suggested topics and couldn't find what I am looking for or is working.

I want to input as many strings as I want (amount not predefined) and as soon as an empty line occurs, the input should stop. What I've tried so far with no success:

while True:
    inp = raw_input()
    if inp.strip() == "":
        break

;

while True:
    inp = sys.stdin.readline()
    if inp == '\n':
        break

;

 while raw_input().strip()!="":
        inp = raw_input()

and such combinations of everything I could find on the Internet. Nothing works so far, any help would be very appreciated!

Upvotes: 0

Views: 1600

Answers (2)

Armen Babakanian
Armen Babakanian

Reputation: 2305

You can check to see if the length of the line is 1 and the ascii character is 10 which is Line feed using ord you can break it then.

Upvotes: -1

chepner
chepner

Reputation: 531055

The two-argument form of iter is almost made for this:

for line in iter(raw_input, ""):
    print "You input", line

Upvotes: 3

Related Questions