Home > Blockchain >  Python - How to convert a password into asterisks while it is being entered by the user?
Python - How to convert a password into asterisks while it is being entered by the user?

Time:11-26

Is there a way in Python to convert characters as they are being entered by the user to asterisks, like it can be seen on many websites?

For example, if an user sets his password as " pass@123" then while entering it should be like

Password : ********

I installed pwinput module but I'm unable to import it. While importing it is showing

import pwinput

"pwinput" is not accessed Pylance

Import "pwinput" could not be resolved Pylance (reportMissingImports)

This is the code where I'm prompting password from the user :

def registration():
    Fname = input("Enter first name: ")
    Lname = input("Enter last name: ")
    while(True):
        gender = input("Enter your gender: ")
        if(gender not in "MmFf"):
            print("THE GENDER YOU HAVE ENTERED IS INCORRECT TRY AGAIN ")
        else:
            break
    age = int(input("Enter your age: "))
    phoneno = int(input("Enter your phone number: "))
    city = input("Enter your city: ")
    email = input("Enter your email id: ")
    while True:
        passwd = input("Enter your password: ")
        repasswd = input("Re-enter your password: ")
        if passwd!=repasswd:
            print("Password doesn't match. Try again!")
        else:
            break
    Q = "insert into registration values ('" Fname  "','" Lname "','" gender "'," str(phoneno) "," str(age) ",'" email "','" passwd "','" city "');"
    mycursor.execute(Q)
    mydb.commit()
    print("Registration done successfully!")

What am I missing here? And what is the correct solution for it?

CodePudding user response:

Like mentioned in my comment, using pwinput will require the least change to your code. After installing pwinput with pip install pwinput import it in your code and replace the corresponding occurences of input:

from pwinput import pwinput

def registration():
    Fname = input("Enter first name: ")
    Lname = input("Enter last name: ")
    while(True):
        gender = input("Enter your gender: ")
        if(gender not in "MmFf"):
            print("THE GENDER YOU HAVE ENTERED IS INCORRECT TRY AGAIN ")
        else:
            break
    age = int(input("Enter your age: "))
    phoneno = int(input("Enter your phone number: "))
    city = input("Enter your city: ")
    email = input("Enter your email id: ")
    while True:
        passwd = pwinput("Enter your password: ")
        repasswd = pwinput("Re-enter your password: ")
        if passwd!=repasswd:
            print("Password doesn't match. Try again!")
        else:
            break
    Q = "insert into registration values ('" Fname  "','" Lname "','" gender "'," str(phoneno) "," str(age) ",'" email "','" passwd "','" city "');"
    #mycursor.execute(Q)
    #mydb.commit()
    print("Registration done successfully!")

registration()

This hides the password entry with * by default:

Enter first name: John 
Enter last name: Doe
Enter your gender: m
Enter your age: 32
Enter your phone number: 12345
Enter your city: london
Enter your email id: abcde
Enter your password: *********
Re-enter your password: ******
  • Related