-4

I've been searching around nearly all morning looking for a piece of code that can help me here but its hard to find one that is similar! I have to create a bank system that asks the user to input a username and password. If these are entered 3 times the system shuts down. So far, i have got my program to know if the password/username is correct or not. Now i just need to figure out how to make it run and stop after 3 incorrect attempts. Really appreciate any help given on this one! Thanks

Code:

username = "bank_admin"
password = "Hytu76E"

usernameGuess = raw_input("Please enter your username: ")
passwordGuess = raw_input("Please enter the password: ")


while (username != usernameGuess or password != passwordGuess):
    print ("Please try again.")

    usernameGuess = raw_input("Please enter your username: ")
    passwordGuess = raw_input("Please enter your password: ")

print ("Password accepted. Access Authorized.")
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • 5
    First: never store a password in plain text, use a hash for it. Second: quitting a while loop is one of the most basic questions, so if you searched around all morning, you either lie or need to improve your search skills. – tamasgal Nov 20 '14 at 14:41
  • If I paste the exact title of your question into Google, the second hit is [the answer to your question, on SO, in Python](http://stackoverflow.com/questions/13293269/how-would-i-stop-a-while-loop-after-some-amount-of-time) (the first hit is your question itself, of course). It's very hard to believe you made a halfway serious effort to google this. – 15ee8f99-57ff-4f92-890c-b56153 Nov 20 '14 at 14:52
  • ive just done the same and ive been on most of the websites returned! Ive edited my code to be like the answers im getting and just keep getting errors! Im not the best at programming, still trying to get the hang of it sorry – Yvonne Higgins Nov 20 '14 at 15:02

1 Answers1

1

You can add a counter to see how many times they guessed the wrong password. Then use that as another condition in your while loop.

incorrectGuesses = 0
correct = False
while (not correct and incorrectGuesses < 4):
    usernameGuess = raw_input("Please enter your username: ")
    passwordGuess = raw_input("Please enter your password: ")
    correct = ((username == usernameGuess) and (password == passwordGuess))
    if not correct:
        print ("Please try again.")
        incorrectGuesses += 1
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218