Here’s a program that asks for a password and loops repeatedly until you enter the correct password.
PASSWORD = "hello"
while True:
password = input("Enter your password: ")
if password == PASSWORD:
break
else:
print("Password incorrect")
print("Password correct.")
Enter your password: ljlkj
Password incorrect
Enter your password: letmein
Password incorrect
Enter your password: hello
Password correct.
Some important points about this program:
- The uppercase PASSWORD variable is uppercase to signify that it should not be changed; it is used only to store the correct password. In other languages this would be called a “constant“, but Python has no special mechanism for creating constants.
- Since Python is case-sensitive, the PASSWORD and password variables are two completely different variables.
- The program creates an “infinite loop” which is only terminated using break when the user enters the correct password.
Leave a Reply