0

I tried this but doesn't work in my case, I want to convert it in a function that prints slow but if I hit enter just completes the whole sentence. I tried this but doesn't work.

import time
import sys
from threading import Thread

def controlled_print(str):
    # Function to skip slow printing
    waiting_time = 0.08
    def check():
        i = input()
        global waiting_time
        waiting_time = 0
    Thread(target = check).start()
    
    # Function to do slow print
    def print_slow(str):
        for letter in str:
            sys.stdout.write(letter)
            sys.stdout.flush()
            time.sleep(waiting_time)
    print_slow(str)

controlled_print('This sentence should print instantly if I hit "Enter".')
Noles
  • 1
  • 1
  • 2
    You never call `print_slow` here. – Tim Roberts Mar 02 '23 at 22:55
  • 2
    Your code is with errors, please display working code and your tries to fix the problem. – RifloSnake Mar 02 '23 at 23:27
  • I just fixed the errors and found my main concern... inside the "check" function I used "global" when there is no global waiting time variable, I fixed that and everything is working as expected. I guess I need to answer myself. @TimRoberts Why? I'm new here, and want to learn. – Noles Mar 03 '23 at 04:57
  • Your original code defined the `print_slow` function, but never called that function. Your latest edit does call it. – Tim Roberts Mar 03 '23 at 06:45
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Mar 03 '23 at 15:23

0 Answers0