英文:
Prevent Input from Being Printed in Python 3
问题
我想知道是否可以阻止Python 3中的输入被打印。
在上面的代码中,例如,如果我输入“236”,控制台会打印:
236
Even
我希望输出只是:
Even
谢谢!
我听说在Python 2.x中使用raw_input()函数可以实现这个。
英文:
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.
答案1
得分: 4
如果您键入输入,它将在Python看到之前通过终端显示在屏幕上。
要抑制这一点,请使用getpass
,通常用于密码输入:
from getpass import getpass
x = int(getpass(prompt=''))
请注意,您仍然会看到换行符被打印,导致空行。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论