0

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?

Ansu Sinha
  • 27
  • 1
  • 8
  • 1
    What code are you using to prompt the user for a password? Can you include a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)? – Dash Nov 25 '22 at 05:40
  • @Dash I have added the code snippet. – Ansu Sinha Nov 25 '22 at 09:47
  • Does this answer your question? [How do I convert a password into asterisks while it is being entered?](https://stackoverflow.com/questions/35805078/how-do-i-convert-a-password-into-asterisks-while-it-is-being-entered) – Tranbi Nov 25 '22 at 12:22
  • The simplest is probably using [pwinput](https://stackoverflow.com/a/73324038/13525512) – Tranbi Nov 25 '22 at 12:26
  • @Tranbi I have installed pwinput but its not working. – Ansu Sinha Nov 25 '22 at 14:06

1 Answers1

0

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: ******
Tranbi
  • 11,407
  • 6
  • 16
  • 33
  • As mentioned in question body I have successfully installed " pwinput " module but during import Python is showing "pwinput" is not accessed Pylance. – Ansu Sinha Nov 25 '22 at 17:19
  • 1
    This hints at a problem with your environment. Do you have an ImportError when you try to run the code? Do you use vscode? Check your python version in the console and in vscode. Also run `python -m pip install pwinput` instead. – Tranbi Nov 25 '22 at 17:47
  • Yes I use VS Code. I didn't face any ImportError instead it is showing Anshus-MacBook-Air:Test file ansusinha$ /usr/local/bin/python3 "/Users/ansusinha/Desktop/Test file/sql.py" Traceback (most recent call last): File "/Users/ansusinha/Desktop/Test file/sql.py", line 1, in import pwinput ModuleNotFoundError: No module named 'pwinput' And when I run this : pip install pwinput it gives requirement already satisfied – Ansu Sinha Nov 25 '22 at 18:04
  • The problem is resloved now. But simultaneously new problem got raised that pwinput module is not callable – Ansu Sinha Nov 25 '22 at 18:25
  • See the first line of my code. Thé function you're trying to use is called pwinput inside the pwinput module. You either import the module (`import pwinput`) and call the function with `pwinput.pwinput(...)`. Or `from pwinput import pwinput` and call the function directly: `pwinput(...)` – Tranbi Nov 25 '22 at 18:31
  • All problem resolved..!! – Ansu Sinha Nov 26 '22 at 07:18