Python 1 printing an input letter-by-letter with sys



我正在测试sys模块在同一行上逐字母打印字符串的能力。当我尝试以这种方式打印输入时,它会一直工作,直到打印出"none"为止。我对sys的了解还不足以发现和纠正问题。我试着在这个网站上找到一个类似的问题,但只找到了Python 1以外的编码语言的答案。

这是我写的代码:
###imports the sys and time modules###
import sys
import time
###defines a function to print the argument letter-by-letter on one line###
desclist=[]
def liner(prompt):
for i in prompt:
sys.stdout.write(i)
sys.stdout.flush()
time.sleep(0.05)
###prints input prompt through liner function###
x=input(liner("Enter the input here.")

预期结果如下:

Enter the input here.
相反,它打印了以下内容:
Enter the input here.None

我找到了一种可行的方法。在行符(提示符)运行之后,它保持在同一行,除非使用了n:

liner("Enter the input here.")
x=input()

然而,我仍然想知道是否有一个单行的解决方案,这样我就不需要每次打印字符串时都输入多行代码。

最新更新