0

I want to print a line before it is spoke. When I run the program in IDLE, it works fine. But after compilation, the text is spoke first and then printed. Can anyone help me with it?

I tried different ways to type the same code but it didn't work.

import pyttsx3
engine = pyttsx3.init()
engine.setProperty('rate', 150)
engine.setProperty('volume', 1)
def say(y):
        print(y, end='')
        engine.say(y)
        engine.runAndWait()
say('Hello World')

I want to print a line before it is spoke but it is always spoke first after compilation.

  • 1
    try adding `flush=True` to your `print` statement. – John Anderson Apr 13 '19 at 04:01
  • First, what do you mean by "compilation", python is a scripting language and it doesn't compile. Second, your print statement is before your playback so, your print statement always works as you desire. Maybe you could put a `sleep` after your `print(y)` would clarify this. – Masoud Rahimi Apr 13 '19 at 04:01
  • When you convert your program to executable format, it is called compilation – Chaitanya Lakhchaura Apr 13 '19 at 05:11

2 Answers2

0

In some environments, Python's print statement gets "buffered" and not actually printed until there's an idle time.

Try adding

import sys

and then doing

print(y, end='')
sys.stdout.flush()

Flushing stdout will force Python to actually display the printed text before continuing.

Luke
  • 5,329
  • 2
  • 29
  • 34
0

@JohnAnderson 's comment in the main question did exactly what I wanted. Addingflush=Truein print statement did it