printing slowly (Simulate typing)

匿名 (未验证) 提交于 2019-12-03 01:52:01

问题:

I am trying to make a textual game in python. All goes well however, I would like to make a function that will allow me to print something to the terminal, but in a fashion hat looks like typing.

Currently I have:

def print_slow(str):     for letter in str:         print letter,         time.sleep(.1)  print_slow("junk")

The output is:

j u n k

Is there a way to get rid of the spaces between the letters?

回答1:

In Python 2.x you can use sys.stdout.write instead of print:

for letter in str:     sys.stdout.write(letter)     time.sleep(.1)

In Python 3.x you can set the optional argument end to the empty string:

print(letter, end='')


回答2:

This is my "type like a real person" function:

import sys,time,random  typing_speed = 50 #wpm def slow_type(t):     for l in t:         sys.stdout.write(l)         sys.stdout.flush()         time.sleep(random.random()*10.0/typing_speed)     print ''


回答3:

Try this:

def print_slow(str):     for letter in str:         sys.stdout.write(letter)         sys.stdout.flush()         time.sleep(0.1)  print_slow("Type whatever you want here")


转载请标明出处:printing slowly (Simulate typing)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!