MP: The Really Rubbish Password Program

This is an example mini-program to demonstrate the use of an if ... else statement.

The aim is to ask the user for a password and if the password matches the secret password then the user gets into the system, if not the user is denied access.

Feel free to play around with the program as much as you like.

Planning our program in pseudocode.

Set the secret password

Ask the user to enter their password
if the input matches the secret password
  grant access
else
  do not grant access

1. The Complete Program

The complete program is given below and then explained in the subsequent sections.

# The Really Rubbish Password Program

# This is the stored password for the user
secret_password = "secret"

print("Welcome to NOSA Inc.")
print("Did you know that the Moon is an average of 238,855 miles away from Earth\n")

password = input("Please enter your password:\n")

if password == "secret":
  print("\nAccess Granted!")
else:
  print("\nAccess Denied!")

input("\n\nPress the enter key to exit.")

2. Breaking Down the Program

The following section explains the two main parts of the program.

2.1 Getting the User Input.

The first part of the code prompts the user for their password and stores it in the variable password.

# The Really Rubbish Password Program

# This is the stored password for the user
secret_password = "secret"

print("Welcome to NOSA Inc.")
print("Did you know that the Moon is an average of 238,855 miles away from Earth\n")

password = input("Please enter your password:\n")

2.2 Testing the Password

The second part of the code then uses the variable password to check if the password is correct. If the condition password == "secret" is True then access is granted otherwise (else) access is denied.

if password == "secret":
  print("\nAccess Granted!")
else:
  print("\nAccess Denied!")

input("\n\nPress the enter key to exit.")

References

Dawson, M. (2010). Python programming for the absolute beginner, third edition (3rd ed.). Delmar Cengage Learning.