0

Hello, I'm a starter in Python and I need help. I'm trying to ask the user to input as much text as he/she likes until he/she types EOF (end of file) on a separate line. Once he/she does, the program should end. I started on my code and below is what I have so far. I'm using the 'for' loop in my code, is that correct?

#Set textInput to 0?
textInput = 0
    #Use the 'for' loop to keep looping.
    for i in textInput:
        #Prompt for user text input. Is this in the right spot?
        textInput = input()
        #If the user types EOF on a new line, then end the program.
        if textInput == "\n" and textInput == "EOF":
            break
        #Keep going until EOF is typed. Is this right?
        else:
            continue
2
Contributors
1
Reply
23
Views
3 Years
Discussion Span
Last Post by slate
1

"EOF" means three letters: E,O,F.
This statement is never True:
if textInput == "\n" and textInput == "EOF":

One way to handle this ("\n" cannot be entered) is to catch EOFError:

inputs=list()
while True:
    try:
        s = input()
        inputs.append(s)
    except EOFError:
        print(inputs)
        break

Edited by slate: \n

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.