【问题标题】:How to replace each character of the user's input with asterisks (****) in the IDLE terminalIDLE终端中如何用星号(****)替换用户输入的每个字符
【发布时间】:2016-05-16 20:52:57
【问题描述】:
def check_password(x):
while x != ("admin123"):
    x = input("incorrect password \ntry again:")
return x

password = input("please type the password: ")
password = check_password(password)
print ("correct password")

出于安全原因,我尝试在 IDLE 终端中用星号 (*) 替换用户输入的每个字符,因为用户将输入密码。我找到了一些替代方案,例如 getpass() 模块,但 getpass() 不会在终端 IDLE 上为用户显示任何内容来告诉他/她正在输入。因此,我决定寻找替代解决方案而不是 getpass()。

【问题讨论】:

  • @Gary 该解决方案对我不起作用,因为我在 Windows 上使用 IDLE,它显示(没有名为“termios”的模块),所以这不是我问题的解决方案。
  • @Y.Hasan 用谷歌搜索找到了我this。可能会有所帮助。是的,有时这种重复的标记会杀死实际的非重复问题:-(

标签: python python-3.x passwords


【解决方案1】:

我已经编写了这段代码,它为我完成了这项工作

只需通过 getch() 将用户输入作为一系列字符

不打印输出,然后在每次输入后打印 *

import sys
import msvcrt

passwor = ''
while True:
    x = msvcrt.getch()
    if x == '\r':
        break
    sys.stdout.write('*')
    passwor +=x

print '\n'+passwor

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 2015-03-31
    • 2012-07-01
    • 1970-01-01
    • 2011-11-11
    • 2019-07-02
    相关资源
    最近更新 更多