dyao
dyao

Reputation: 1011

(Python) Creating a basic "enter your password" program

I'm trying to create a basic program which asks for a user to enter and then re-enter a password. If it matches, then it will print "Password created". If it doesn't, it will continue to ask the user to retry until the passwords match.

This is what I have so far; I know a loop and at least one other "if" statements is needed, but I don't know how to do it.

password=raw_input("please select a password")
password_again=raw_input("please re-type your password")
loop=raw_input("Password does not match. Please try again")
if password_again==password:
      print("password created")
else: raw_input(loop)

Help is appreciated!

Upvotes: 0

Views: 9709

Answers (4)

James Sapam
James Sapam

Reputation: 16940

I think using getpass module would be a wise decision: It will Prompt the user for a password without echoing

from getpass import getpass

print "Please select a password: "
passwd = getpass()

print "Please re-type your password: "
if passwd == getpass():
    print "password created"
else:
    print "Password does not match. Please try again"

Output:

Please select a password:
Password:
Please re-type your password:
Password:
Password does not match. Please try again

Upvotes: 1

thefourtheye
thefourtheye

Reputation: 239463

The simplest way is to make use of iter function like this

password = raw_input("please select a password")
for re_password in iter(raw_input, password):
    print "Password does not match. Please try again :",
print "Password accepted!"

Upvotes: 0

zhangxaochen
zhangxaochen

Reputation: 34017

That's not a loop, use while True and break :

while True:
    password=raw_input("please select a password")
    password_again=raw_input("please re-type your password")
    if password_again==password:
        print("password created")
        break
    else:
        print "Password does not match. Please try again"

Upvotes: 0

PyNEwbie
PyNEwbie

Reputation: 4940

This is a great way to learn about while loops

Ask for the values

password=raw_input("please select a password")
password_again=raw_input("please re-type your password")

Loop until they match

while password_again != password:
    print "They don't match please try again"
    password=raw_input("please select a password ")
    password_again=raw_input("please re-type your password ")

Upvotes: 0

Related Questions