How to query an input in Python without outputting a new line

可紊 提交于 2019-12-24 19:10:43

问题


The title describes the question pretty much.


回答1:


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?

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.




回答2:


If you use raw_input it does not insert a new line automatically.

>>> name = raw_input ("What...is your name? ") 
What...is your name? Arthur, King of the Britons!


来源:https://stackoverflow.com/questions/2982964/how-to-query-an-input-in-python-without-outputting-a-new-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!