Simple password rules

From Computer Science Wiki
Jump to: navigation, search
This a problem set for you to work through [1]

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

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

# 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

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 applyUse an idea, equation, principle, theory or law in relation to a given problem or issue. those rules to your password form, above.

How you will be assessed

Every problem set is a formative assignment. Please click here to see how you will be graded

References

One Possible Solution

Click the expand link to see one possible solution, but NOT before you have tried and failed!

 1 # this is my password rules application
 2 
 3 print ('''Please enter a password. 
 4 
 5 The password must: 
 6     
 7     1. have 8 characters of more
 8     2. have at least one number
 9     3. have at least one symbol
10     
11     ''')
12 user_password = raw_input("Please enter your password: ") 
13 confirm_user_password  = raw_input("Please confirm your password: ")   
14 
15 
16 if user_password != confirm_user_password:
17     print("Error: your passwords do not match.")   
18 else: 
19     if len(user_password) < 8:
20         print("Error: Your password must be at least 8 characters long. ") 
21     else: 
22         print("Length is ok. Now lets now check for numbers.") 
23         if user_password.isalpha() == True:
24             print("Error: you must have at least one number in your password.")
25         else:
26             print("let's see if there is a character in there.") 
27             if user_password.isalnum() == True:
28                 print("Error: You need punctuation marks")
29             else:
30                 print("You have passed all our required tests, and have created a valid password.")