使Text看起来像是在Python Shell中输入的

时间:2014-05-30 02:36:42

标签: python python-3.x time python-3.3

到目前为止,我提出的唯一方法是清除控制台,然后再显示一个字母的字符串。建议?

当前代码:

import os
import time
In=list(input("Text to be displayed: "))
Out=""
Move=[]
print("-")
while len(Move)<len(In):
    Move+=["/","|","\\","-"]
for a in range(0,len(In)):
    if In[a]==" ":
      x=.3
    elif In[a] in ",';:!@#$%^&*()_+-=[]{}":
      x=.25
    elif In[a] in "aeiouzxcvbnm1234567890AEIOUZXCVBNM":
      x=.2
    else:
      x=.15
    os.system('cls')
    Out+=In[a]
    print(Out+Move[a])
    time.sleep(x)
os.system('cls')
print(Out)
input()

2 个答案:

答案 0 :(得分:3)

只需使用print()end=''保持同一行,flush = True作为时间:

import time
Move+=["/","|","\\","-"]
text = input('Text to be displayed: ')
for i in text:
    print(i, end='', flush=True)
    time.sleep(0.1)
print('\n')

运行如下:

bash-3.2$ python3.3 test.py
Text to be displayed: This is a test
This is a test

bash-3.2$ 

解决您的评论,请使用以下代码:

import time
Move=["/","|","\\","-",""]
text = input('Text to be displayed: ')
for i in range(len(text)):
    print(text[i]+Move[i%5], end='', flush=True)
    time.sleep(0.1)
print('\n')

答案 1 :(得分:2)

如果我正确理解了这个问题,你只希望字符串一次出现一个字符,每个字符之间有一个延迟。

import time
from sys import stdout

In=list(input("Text to be displayed: "))
for x in In:
  print(x, end='')
  stdout.flush()
  time.sleep(0.1)

print("\n")