-1

I am trying to write a simple block of python code that I would like to reuse in future programs. It will check to see if a password is correct and if it is not return to the first line of code and start the process again. The way I am attempting to accomplish that gives me an error. Can anyone help me find a better solution?? Here is the code I am currently using:

def password_checker():
   program_acceptance = "Welcome to the Program!  "
   acceptable_password = "Godisgood"
print("Please Enter the Password")
while True:
password = input("Password:  ")
if password == acceptable_password:
    print(program_acceptance)
    break
if password != acceptable_password:
    print("Invalid password, please try again..."
    break
TriumphLife
  • 9
  • 1
  • 2

1 Answers1

1

That last break statement should be removed to ensure that the program keeps looping when a false password is provided.

def password_checker():
    program_acceptance = "Welcome to the Program!  "
    acceptable_password = "Godisgood"
    print("Please Enter the Password")
    while True:
        password = input("Password:  ")
        if password == acceptable_password:
            print(program_acceptance)
            break
        if password != acceptable_password:
            print("Invalid password, please try again...")

password_checker()
Xukrao
  • 8,003
  • 5
  • 26
  • 52