如何在Python 3中使用input()来获得回车



简介

我正试图运行一个脚本,该脚本涉及从用户输入中取名字;DONE";,但我希望用户输入保持在同一行,直到输入变成这样

我想要什么

Enter a name (John D) or DONE
----------
Adam B #after input, turns blank and waits for another name on same line 
Bob D #on same line as mentioned before
Done #on same line as mentioned before
----------
#finish code

我得到的

Enter a name (John D) or DONE
----------
Adam B # each iteration still newlines
Bob D # newline
Done #newline 
----------
#finish code

故障排除

我浏览了这里和其他网站上的论坛,只找到了与print()和单个输入有关的答案,而不是一个或多个输入。我很惊讶我是唯一一个有这个问题的人,这让我怀疑我是否忽略了一些显而易见的东西。

代码段

. . .
done = False
i = 0
prompt = "Enter a name (John D) or DONEn----------nr"
print(prompt, end='')

#start loop for entering names as needed, aka 'until they type done'

while done == False:
#ask what to do, and convert whatever they type into all uppercase      
usrinput = input('' + 'r').upper()
#check if user typed done
if usrinput != "DONE":
#if not, use i variable to manually iterate and add each name to the placeholder "custlog"
custlog[i] = usrinput
#print current name for reference or debug, then add 1 for each iteretion
#print("%sr" % custlog[i])
i = i + 1
#but if they did enter done..
elif usrinput == "DONE":
. . .

我认为唯一的方法就是"手动";向上走一行,然后删除它:

usrinput = input().upper()
print ('33[1A33[K', end='')

或使用sys库:

import sys
usrinput = input().upper()
sys.stdout.write('x1b[1A')
sys.stdout.write('x1b[2K')

最新更新