如何在Python中查询输入而不输出新行 [英] How to query an input in Python without outputting a new line

查看:103
本文介绍了如何在Python中查询输入而不输出新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题几乎描述了这个问题.

The title describes the question pretty much.

推荐答案

执行查询的input函数不会发出换行符:

The input function, which does the query, does not emit a newline:

>>> input('tell me: ')
tell me: what?
'what?'
>>> 

如您所见,

,输出的提示没有任何换行符,此后用户键入的内容与提示出现在同一行.当然,用户也在键入换行符,并且(与用户键入的其他所有内容一样), 换行符也得到了回显(因此,下面的行将显示更多结果).那是你的问题吗?

as you see, the prompt is output without any newline, and what the user types after that appears on the same line as the prompt. Of course, the user is also typing a newline, and (like everything else the user types), that newline is echoed (so further results are on following lines). Is that your problem?

如果是这样,则需要切换到特定于平台的方法,例如 msvcrt 在Windows上(或者,您可以在Windows上寻找一个curses端口,但我不知道Python 3是否有一个端口).这两个模块非常不同,您还没有弄清您的平台(或您的确切需求-我的上一段是有根据的猜测;-),所以我只等您阐明需求和平台,而不是而不是发表可能没有帮助的长篇论文.

If so, then you need to switch to platform-specific approaches, such as curses on just about any machine except Windows, and msvcrt on Windows (or, you could look for a curses port on Windows, but I don't know if there's one for Python 3). The two modules are very different, and you haven't clarified your platform (or your exact needs -- my previous paragraph is an attempt at an educated guess;-), so I'll just wait for you to clarify needs and platforms rather than launching into long essays that may not prove helpful.

这篇关于如何在Python中查询输入而不输出新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆