小编Sar*_*rah的帖子

模糊用户输入

我试图创建一个程序,采取用户输入,而不是显示实际输入我想用*替换输入

我已经尝试使用此代码,但我不断收到下面的错误,我将不胜感激任何指导或帮助.

import msvcrt
import sys


def userinput(prompt='>'):
    write = sys.stdout.write
    for x in prompt:
        msvcrt.putch(x)

    entry = ""

    while 1:
        x = msvcrt.getch()
        print(repr(x))

        if x == '\r' or x == '\n':
            break
        if x == '\b':
            entry = entry[:-1]
        else:
            write('*')
            entry = entry + x
    return entry

userEntry = userinput()
Run Code Online (Sandbox Code Playgroud)

错误:

Traceback (most recent call last):
  File "C:\Users\Mehdi\Documents\Teaching\KS5\AS CS\getPass.py", line 24, in <module>
    userEntry = userinput()
  File "C:\Users\Mehdi\Documents\Teaching\KS5\AS CS\getPass.py", line 9, in userinput
    msvcrt.putch(x)
TypeError: putch() argument …
Run Code Online (Sandbox Code Playgroud)

python input

5
推荐指数
1
解决办法
299
查看次数

标签 统计

input ×1

python ×1