Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Printing slowly (to mimic typing)

I’m trying to make a text based game and I want the text to print out slowly to simulate typing. I also want it to be an input. Is it possible to do this?

I saw someone else use this code(bolded is what i added):

import sys,time

def sinput(str):
  for c in str + '\n':
    sys.stdout.write(c)
    sys.stdout.flush()
    time.sleep(4./90)

slowtest=sinput('Does it work? ')
**if slowtest == 'yes':
    print('Amazing!')**

And it was able to print out the code slowly just like I wanted, unfortunately I can not answer the question ("Does it work?").

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can assign input() to slowtest.

sinput() function is not returning anything.

Once you print slowly you can use input() for slowtest variable then check the if condition.

import sys,time

def sinput(str):
  for c in str + '\n':
    sys.stdout.write(c)
    sys.stdout.flush()
    time.sleep(4./90)

sinput('Does it work? ')
slowtest = input()
if slowtest == 'yes':
    print('Amazing!')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading