0

I would like to know if it is possible to prevent an input from being printed in Python 3.

x = int(input())

def func(n):
    if n%2==0:
        print("Even")
    else:
        print("Odd")

func(x)

In the code above, for instance, if I input "236", the console prints:

236
Even

I would like the output to be just:

Even

Thanks!

I've heard this was possible in Python 2.x with the raw_input() function.

  • 2
    FYI: the [release notes](https://docs.python.org/3/whatsnew/3.0.html#builtins) for Python 3.0 say "`raw_input()` was renamed to `input()`. That is, the new `input()` function reads a line from `sys.stdin` and returns it with the trailing newline stripped...", which is to say that Python 3.x `input()` *is* `raw_input()` – JRiggles May 31 '23 at 18:17

1 Answers1

4

If you type an input, it's being displayed on screen by your terminal before Python even sees it.

To suppress this, use getpass, which is normally used for password input:

from getpass import getpass

x = int(getpass(prompt=''))

Note that you still see the newline being printed, resulting in an empty line.

Thomas
  • 174,939
  • 50
  • 355
  • 478