Search code examples
pythoninputprintingread-eval-print-loopsys

i am learning python, please can someone tell me how to fix it so i can slowly print each letter (like a type writer) but also working with the input


door1 = input('\n\nthere are 3 doors ahead of you, \n\nwhich one do you pick? \n\n1,2 or 3.')
for char in door1:
    sys.stdout.write(char)
    sys.stdout.flush()
    time.sleep(0.1)

I am trying to get the code to slowly print the question, however I don't know how to do this when I am trying to get an input at the same time.


Solution

  • You almost had it!

    import time,sys
    door1 = '\n\nthere are 3 doors ahead of you, \n\nwhich one do you pick? \n\n1,2 or 3.'
    for char in door1:
        sys.stdout.write(char)
        sys.stdout.flush()
        time.sleep(0.1)
    response = input()
    

    Before, you were writing the user response the typewrite-style. You wanted to have the question written like that, so I changed door1 into you question string. Then after printing it slowly, I put the input function there.