0

I am developing a text-based game on Python and I wanted to have the effect where letters appear one at a time. It has to be a function because I wanted the effect to apply to almost all printed strings. I am using the code seen below, which I got from here, and it works fine for this simple example, but the problem is that it does not recognize characters like apostrophes or hyphens and it does not retain the line breaks I have already set up, so it does not work for longer amounts of text.

Is there a way to get around this? If I could have it at least recognize more characters and have it print on a new line every time I use a new slow() function, that would be great.

import sys, time    
def slow(text, delay=0.02):
    for c in text:
        sys.stdout.write(c)
        sys.stdout.flush()
        time.sleep(delay)
    print
slow("Hello!")

Thank you and apologize for the beginner question.

UpmostScarab
  • 960
  • 10
  • 29
Nolan__
  • 3
  • 3
  • 1
    This code absolutely will work on apostrophes. If you're having trouble with those, then there must be something else wrong. Please post a full code example that demonstrates an actual problem. – John Gordon Feb 17 '23 at 16:13
  • @JohnGordon is absolutely correct. Also note that *print* on its own is effectively a noop – DarkKnight Feb 17 '23 at 16:15
  • @JohnGordon You're right--I realized the text I was trying it on had been copied from something in Microsoft Word, where the apostrophes were formatted differently and the hyphens became em-dashes. Thank you for pointing that out – Nolan__ Feb 17 '23 at 16:47

3 Answers3

0

This can all be done with print()

It is a requirement that a newline is output after the individual characters. However, the input string may already end with '\n' so don't repeat it.

from sys import stdout
from time import sleep 

def slow(text, delay=0.1):
    if text: # only process if the string is not zero length
        for c in text:
            print(c, end='', flush=True)
            sleep(delay)
        if text[-1] != '\n':
            print()

slow("Hello world!")
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
-1

Try this

import sys, time     
def slow(text, delay=0.5): 
  for c in text: 
     sys.stdout.write(c) 
     sys.stdout.flush() 
     time.sleep(delay) 
   print('\n')
slow("Hello!\nAlejandra R-")
slow("Hello!\nAlejandra R 2")
-2
import time
def print_one_at_a_time(text, sleep=0.1):
    "print a letter at a time, sleep certain seconds in between"
    text += '\n'
    for c in text:
        print(c, end='', flush=True)
        time.sleep(sleep)
D. Zhai
  • 41
  • 7
  • This does **not** do what OP is trying to achieve – DarkKnight Feb 17 '23 at 16:19
  • @Pingu What is OP? and which part am I missing? – D. Zhai Feb 17 '23 at 16:21
  • OP == Original Poster - i.e., the person asking the question. The intention is to introduce a delay between the output of each character. As you don't flush the stream then there's no delay – DarkKnight Feb 17 '23 at 16:26
  • Thanks for explaining, I don't think the flush is necessary, at least it doesn't make any difference for me. but I explicitly add that to the print. When does that flush to False and flush to True make any difference?@Pingu – D. Zhai Feb 17 '23 at 16:33