【问题标题】:Not displaying Input in Python在 Python 中不显示输入
【发布时间】:2018-08-15 15:09:44
【问题描述】:

我想问一下我们可以对输入函数进行哪些更改,以便我们输入的任何内容都表示为“*”。正如我们都看到的,当我们在登录页面上输入密码时,它会写成******* .这可以在python中完成吗? 像这样:

Python 3.6.6 (v3.6.6:4cu1f74eh7, Jun 27 2018, 02:47:15) [MSC v.1900 64 bit 
(Intel)] on win64
Type "copyright", "credits" or "license()" for more information.
>>> a = input('Write your password here:')
Write your password here: ************* # we write our password
>>> print(a)
stackoverflow

The getpass is not working and giving a warning too.

【问题讨论】:

  • 查看docs,您可以读到如果无回显输入不可用,getpass() 会退回到打印警告消息以流式传输并从 sys.stdin 读取并发出 GetPassWarning。
  • @toti08 谢谢!现在我知道了

标签: python python-3.x passwords python-3.6


【解决方案1】:

您可以使用以下库来做到这一点

import getpass

pswd = getpass.getpass('Write your password here:')
print(pswd)

【讨论】:

  • 为什么什么都不做
  • 它隐藏了输入,当你按下回车键时,它正在打印你所做的输入
【解决方案2】:

使用getpass

喜欢

import getpass s = getpass.getpass("Write your password here:") Write your password here: # input will be hidden

【讨论】:

  • 请注意@argo 已经提供了类似的答案,避免添加重复。
【解决方案3】:

您可以在 python 中使用getpass 模块。

a = getpass.getpass("Write your password here:")

当您执行上述操作时,您将获得以下信息:

Write your password here: #No Characters Shown

但是,您可以编写一个函数来存储输入的文本,并在调用时只报告一串 *。有点像this,不是我写的

【讨论】:

  • 请注意@argo 已经提供了类似的答案,避免添加重复。
猜你喜欢
  • 1970-01-01
  • 2018-05-15
  • 1970-01-01
  • 2021-03-23
  • 2012-02-04
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 2023-03-06
相关资源
最近更新 更多