-1

thanks for taking the time to help me out. I have been learning Python 3.2 this past semester, so my knowledge isn't anything more than a newbie. I am on Windows 7, using Python 3.2 and using Python's IDLE.

I am using a for loop to print a series of strings in a scrolling-like fashion. What I mean is that IDLE will print the string letter by letter. Below is the code I am using.

import os, sys, time

message=('Enter string in here. It will print what is in here letter by letter.')

for line in message:
    print(line,end='')
    sys.stdout.flush()
    time.sleep(0.03)

My question is: Is there a way to have the entire string (message) be printed if the user were to hit 'enter' anytime throughout the time the string is being printed. So even if the user were to hit 'enter' at the beginning; the entire string would be printed.

Once again I like to thank you for taking the time to help me. It's much appreciated.

dhc
  • 61
  • 1
  • 1
  • 6

1 Answers1

-1

Your code is slightly confusing because your loop talks about lines, but isn't working on lines.

message=('Enter string in here. It will print what is in here letter by letter.')

So message is a string

for line in message:

If we iterate over a string we get characters. So each time round the loop we'll set line to the next character. Naming variables like that just confuses anyone who reads it, including yourself.

Is there a way to have the entire string (message) be printed if the user were to hit 'enter' anytime throughout the time the string is being printed. So even if the user were to hit 'enter' at the beginning; the entire string would be printed.

I think what you're asking is whether it's possible to be immune to enter moving the cursor onto the next line. This question details a number of methods of doing that, and wim has an example of one in his answer. It's only a partial solution though and for this type of thing I'd favour using curses (see link).

If that's not what you're asking, then I think you need to re-word your question.

Community
  • 1
  • 1
Paul S
  • 7,645
  • 2
  • 24
  • 36