Simple password rules
![](/images/d/d1/Square.png)
This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are to HELP YOU THINK THROUGH problems. The solution is at the bottom of this page, but please don't look at it until you have tried (and failed) at least three or four times.
The Problem[edit]
This problem is designed to test your skill and knowledge of strings, specifically your knowledge and understanding of testing if strings are a certain length, contain numbers, etc...
Please write a simple program that asks the user to enter a password and confirm a password. The password must be at least 8 characters long, have one uppercase character, and have at least one symbol. Of course, the original password must match the confirmation password. :-)
Some Code to Get You Started[edit]
# this is my password rules application
print ('''Please enter a password.
The password must:
1. have 8 characters of more
2. have at least one number
3. have at least one symbol
''')
user_password = raw_input("Please enter your password: ")
confirm_user_password = raw_input("Please confirm your password: ")
# the first check will be to see if the passwords match.
if user_password != confirm_user_password:
print("Error: your passwords do not match.")
# let's see if the password is at least 8 characters. I wonder how we would do that?
#
else:
print("let's see if the password is at least 8 characters.")
Take This Farther[edit]
This simple program is very useful, and it is likely that you have used a function when you register for a web service.
- password rules should be customizable! Make a configuration form that asks the user how long the password should be, how many uppercase characters it should have, and how many symbols, and then apply those rules to your password form, above.
How you will be assessed[edit]
Every problem set is a formative assignment. Please click here to see how you will be graded
References[edit]
One Possible Solution[edit]
Click the expand link to see one possible solution, but NOT before you have tried and failed!
# this is my password rules application
print ('''Please enter a password.
The password must:
1. have 8 characters of more
2. have at least one number
3. have at least one symbol
''')
user_password = raw_input("Please enter your password: ")
confirm_user_password = raw_input("Please confirm your password: ")
# the first check will be to see if the passwords match.
if user_password != confirm_user_password:
print("Error: your passwords do not match.")
else:
if len(user_password) < 8:
print("Error: Your password must be at least 8 characters long. ")
else:
print("Length is ok. Now lets now check for numbers.")
if user_password.isalpha() == True:
print("Error: you must have at least one number in your password.")
else:
print("let's see if there is a character in there.")
if user_password.isalnum() == True:
print("Error: You need punctuation marks")
else:
print("You have passed all our required tests, and have created a valid password.")