1

I have taken an excerpt from my code where the problem still happens. I believe this is because of my slow_type function but while the dialogue is being "slow typed" my input will take any values typed during the "slow_type" even before the input shows up. I want the input to only be able to be typed in once the slow_type function is finished. How would I do this?

import time
import sys

def slow_type(line, speed):#You input the dialogue and speed(smaller = faster)
    for l in line:
        sys.stdout.write(l)
        sys.stdout.flush()
        time.sleep(speed)
    time.sleep(.5)

NL1 = "Huh, I see you finally came. "
NL2 = "You know I sent you that message a long time ago.\n"
NL3 = "But I guess you are here now, and that's what matters. "
NL4 = "So...\n"
NL5 = "\n\t\t\t\tAre you ready?\n\n"

slow_type(NL1, .05)
slow_type(NL2, .05)
slow_type(NL3, .05)
slow_type(NL4, .2)
slow_type(NL5, .05)

print("\t\t1:Yes, I am. 2:Who are you? 3:No, I am leaving.\n")
first_choice = input("\t\t\t\t    ").lower()

I am using Windows 10 cmd.

Roberto Chavo
  • 33
  • 1
  • 5
  • So if the user types before `input` is called, those characters wait until `input` is called, and then display themselves all at once? I have observed this behavior in my own programs. I don't think it's specific to Python, but rather a [feature of the command prompt](https://learn.microsoft.com/en-us/windows/console/console-input-buffer). Perhaps you could erase the console's input buffer before the input call, but this seems difficult and OS-specific. – Kevin Oct 29 '18 at 13:20
  • @roganjosh I just tested in both the shell and windows 10 cmd. In the shell if you type while slow_type is in process it just types in front. In cmd when you type while slow_type is in process then all the character/integers show up once the input appears once slow_type has finished. – Roberto Chavo Oct 29 '18 at 13:23
  • 1
    Possible duplicate of [How to flush the input stream in python?](https://stackoverflow.com/questions/2520893/how-to-flush-the-input-stream-in-python). Also: [Linux : python : clear input buffer before raw_input()](https://stackoverflow.com/questions/26555070/linux-python-clear-input-buffer-before-raw-input) – r.ook Oct 29 '18 at 13:24
  • @Kevin yes that's right. Another problem though is if enter is hit then the game proceeds to the next point once input is called. – Roberto Chavo Oct 29 '18 at 13:25
  • Based on a quick search it seems it'll highly depend on your OS. look into `termios` for Unix and `msvcrt` for Windows. – r.ook Oct 29 '18 at 13:25

0 Answers0