Hi I am trying to write a function password_input(prompt, password) that will repeatedly ask for a password using the given prompt until the given password is entered. I have testing code, but I am completely lost upon how to do it and where to begin.
This is the testing code:

password_input(“Enter your password: “,”dromedary”)
print “Welcome to my computer!”

I know that I have to use a while loop to keep checking the password, but thats about it.
Any help is appreciated, thanks a lot!

This will get you started, but there are many things you need to consider beyond the core function.

def password_input(prompt, pw):
    while True:   # Forever
        data = raw_input(prompt)
        if data == pw:
            return

The key routine raw_input() is no longer recommended, but I've forgotten the replacement riff.

thanks a ton BearofNH I figured out the rest

You might want to look at using hashlib so that your password function is more secure. For example, someone could examine the plaintext of your python and find out what the password is. An easy way to get around this is to use a hashing function like SHA1 to encrypt your password. Then you prompt the user for a password and encrypt it too. Finally, you check if the two encrypted password match. Nobody can reverse engineer your password from your code.

import hashlib

# Here we put the expected value of our password.
# The plaintext of this string is password.
known_good = '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'

user_supplied = hashlib.sha1(raw_input('Password: ')).hexdigest()

if known_good == user_supplied:
  print Good
else:
  print bad

You might want to look at using hashlib so that your password function is more secure. For example, someone could examine the plaintext of your python and find out what the password is. An easy way to get around this is to use a hashing function like SHA1 to encrypt your password. Then you prompt the user for a password and encrypt it too. Finally, you check if the two encrypted password match. Nobody can reverse engineer your password from your code.

import hashlib

# Here we put the expected value of our password.
# The plaintext of this string is password.
known_good = '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8'

user_supplied = hashlib.sha1(raw_input('Password: ')).hexdigest()

if known_good == user_supplied:
  print Good
else:
  print bad

The problem with your technique is that I know your know_good password is the value 'password'.

See http://en.wikipedia.org/wiki/Rainbow_tables

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.