I am trying to create a program that take the user input but rather than displaying the actual input I would like to replace the input with an *
I have tried using this code but I keep getting the error below, I would appreciate any guidance or help.
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()
Error:
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 must be a byte string of length 1, not str