0

I am trying to make a small text creator application using Python. The concept is just the same as ordinary text creator (e.g. notepad). But I got difficulties to allow users to type a lot of paragraphs. So far, I am just able to allow users to type 2 paragraphs. Is there anyone can help me? Here is my script:

print "Welcome to 'Python Flat Text Creator'."
print "Please enter the name of your file and its extension (.doc atau .txt)."

filename = raw_input("> ")
target = open(filename, 'w')
typeyourtext = raw_input("Type below: \n")
target.write(typeyourtext + "\n")
typeyourtext = raw_input("\n")
target.write(typeyourtext + "\n")
target.close()
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Khadis
  • 11
  • 1

2 Answers2

1

An easy answer would be to simply put the typing and displaying of the text in a while(true) block and waiting for something (key press or set of characters) to break the cycle. But I'm not sure if you want to do it this simple.

Try to go around it with inserting the characters one by one as other text editors - take a look at Vim for example. The system which is used there is fairly simple and convenient.

Edit:

Getting keypress: How to accept keypress in command line python?

Do while true cycle: http://wiki.python.org/moin/WhileLoop

at the end of each cycle if the input char isn't chr(27), which is ESC key, then append to the text which you are creating and display it.. but this isn't good for files which are large in size..

Community
  • 1
  • 1
Dropout
  • 13,653
  • 10
  • 56
  • 109
  • Could you give me link(s) to the sample of doing that? I do really a newbie and still have to think hard about the code :) – Khadis May 06 '13 at 13:09
  • edited.. I am sorry that I can't post the full code, but I'm currently at work, so I can't write the whole thing :) hope it helps! – Dropout May 06 '13 at 15:48
  • The links are helpful :) Before this, I haven't learned about "while". Now, I have new references. Thanks :) – Khadis May 07 '13 at 02:53
  • No problem, glad to help you. Just please select an answer so this question will no longer be as "unanswered". Thanks! – Dropout May 09 '13 at 11:31
0

You can use a while loop set to end when the user does not input anything at all.

target = open(filename, 'w')
previousKeypress = 0

print("Type below:\n")

while previousKeypress != "":
    typeyourtext = raw_input("")
    target.write(typeyourtext + "\n")
    previousKeypress = typeyourtext

target.close()

If you intend for users to put additional new lines in the document through no inputs, you can set the condition to react to a certain combination of characters like maybe "abc123" to end it.

You can even ask the user to set this ending combination through another raw_input at the very beginning of the program.

chrtan
  • 1,704
  • 1
  • 13
  • 17
  • How if I want ESC button can stop user from typing words? – Khadis May 06 '13 at 13:14
  • @Khadis As of this point, I am not sure. Using the Escape key to exit the loop can be done using the msvcrt module. The problem is that it appears that the raw_input prevents the escape key from being captured by msvcrt. As of right now, if you really want to use the escape key and have the program run smoothly, I think you might need to build the program using msvcrt to capture the keypresses. Keep in mind though that msvcrt is a Windows-only solution. – chrtan May 06 '13 at 13:40
  • I am currently using Windows :D (and Ubuntu too). Well, the script above has already helped me and taught me new thing :D Thanks. – Khadis May 07 '13 at 02:46