1

I'm trying to create a very simple text editor for an MS-DOS style program I'm making using python. My problem comes when I try to create paragraphs. The way I made it, pressing Enter tells it to save the input. I understand what I did wrong, but how can I fix it and how could I break the input? So far I have this:

def textwriter():
    print("")
    print("Start typing to begin.")
    textwriterCommand = input(" ")
    saveAs = input("Save file as: ")
    with open(saveAs, 'w') as f:
        f.write(textwriterCommand)
4
  • 1
    This might be what you want but I'm not sure how you break the input then. stackoverflow.com/a/3289051/5889975 Commented Feb 19, 2016 at 23:28
  • If you use the answer that @steven linked to, you can watch for two blank lines (lines consisting of a newline only) in a row — perhaps by setting a flag — to break out of the loop. This is commonly how I've seen it handled. Commented Feb 20, 2016 at 1:06
  • I've updated my answer so the examples actually work in the way one would expect, which should be more helpful -- specifically, the second example works perfectly, with newlines Commented Feb 20, 2016 at 5:05
  • This isn't well specified. What is the rule that tells you when the input is done? Commented Feb 6, 2023 at 10:23

2 Answers 2

2

Assign some other EOF sequence, for instance:

EOF_SEQ = 'EOF'

def textwriter():
    print("")
    print("Start typing to begin.")
    buffer = ''
    while EOF_SEQ not in buffer:
        buffer += raw_input(" ") + '\n'
    saveAs = raw_input("Save file as: ")
    with open(saveAs, 'w') as f:
        f.write(buffer)
Sign up to request clarification or add additional context in comments.

1 Comment

Change raw_input to input; OP's using Py3
0

@zamuz's answer is good, and good for simple things like this, but when your code gets more complex, I prefer to use something like input_constrain, which actually tests each keypress to decide what to do next. (Full disclosure: I wrote this.)

For example, to read input until the user presses | (vertical bar):

from input_constrain import until

def textwriter():
    print("")
    print("Start typing to begin.")
    textwriterCommand = until("|")
    saveAs = input("Save file as: ")
    with open(saveAs, 'w') as f:
        f.write(textwriterCommand)

Or, to read input until CTRL - C or CTRL - D (or any other unprintable control char):

from input_constrain import until_not
from string import printable as ALLOWED_CHARS

def textwriter():
    print("")
    print("Start typing to begin.")
    textwriterCommand = until_not(ALLOWED_CHARS, count=5000, raw=True)
    saveAs = input("Save file as: ")
    with open(saveAs, 'w') as f:
        f.write(textwriterCommand)

This uses an optional count parameter, which will stop reading after exactly this many keypresses. raw=True is important because otherwise, CTRL - C and CTRL - D will throw exceptions. By using raw, we can break and continue the task at hand.


Important: You need commit 06e4bf3e72e23c9700734769e42d2b7fe029a2c1 because it contains fixes but no breaking changes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.