Prevent Python from showing entered input

柔情痞子 提交于 2021-02-16 20:59:11

问题


In Python when I change the value of a variable through raw_input in the terminal it would write a new line stating its new value. I wanted to know if there is a way to avoid that because straight after user's input there is a print function which uses the value received before. In the end the user will get both values: original and modified while for the purpose of my program he only has to see the modified.

I use IDLE to write my scripts and then execute them in terminal.

Update

A simple example would be the following. The input is string "Hello" and the output should be "Me: Hello". There should be no "Hello" before the final result.

a = raw_input()
print ("Me: " + a)

And the output should be just

Me: Hello

rather than

Hello
Me: Hello

回答1:


if you want user to see the input, just mute sys.stdout (it's a bit hack tho):

>>> from StringIO import StringIO
>>> import sys
>>> orig_out = sys.stdout
>>> print 'give me input: ',
give me input: 
>>> sys.stdout = StringIO()
>>> a = raw_input('')
string I type
>>> sys.stdout = orig_out
>>> a
>>> 'string I type'

...and if you put this into a function then it's exactly what you want!

a.py

....

def foo():
    orig_out = sys.stdout
    print 'give me input: ',
    sys.stdout = StringIO()
    a = raw_input()
    sys.stdout = orig_out

if __name__ == '__main__':
    foo()

running:

yed@rublan $ ~ python a.py

give me input: something I type!!!!!

yed@rublan $ ~



回答2:


I know this is a late answer, but here is a Python 2+3 solution (with a small improvement):

In prompt_demo.py:

# coding: utf-8
from __future__ import absolute_import
from __future__ import print_function

import io
import sys

import six.moves


def prompt(msg):
    print(msg, end=u'')
    orig_out = sys.stdout
    try:
        sys.stdout = io.StringIO()
        return six.moves.input(msg)
    finally:
        sys.stdout = orig_out


if __name__ == '__main__':
    text = prompt(u"Give me input: ")
    print(u"Do you mean '{0}'?".format(text))
  • This solution uses io.StringIO, which is the Python 2+3 compatible.
  • It also uses the six library to backport the raw_input function (which is renamed input in Python 3).
  • The try ... finally solution is better in case of exception during the input: it restores the original value of sys.stdout.

I also want to point the nice solution available in the Click library. Here is a demo:

import click

if __name__ == '__main__':
    text = click.prompt(u"Give me input: ", prompt_suffix='')
    click.echo(u"Do you mean '{0}'?".format(text))

See the documentation about click.prompt.




回答3:


You don't have to do difficult things like that, the answser is really simple:

chat_respond="you say: "+input("Write here to respond: ")
#i will suppose that you want to write hello
#That will display:
Write here to respond: hello

Because automaticaly the input is printed, it will show the string of the variable but you add something before the input so it will print that plus the input

you say: hello

You don't need to use difficult librairies or modify a module, just to add to the variable before you stock the input, you can also do that:

chat_respond="you say: "
chat_respond+=input("Write here to respond: ")

It will do the same Bye!



来源:https://stackoverflow.com/questions/24087290/prevent-python-from-showing-entered-input

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