180 likes | 439 Views
CMSC 201 – Lab 3. Strings. Overview. Objectives for today's lab: Obtain experience using strings in Python, including looping over characters in strings and conversion from ASCII
E N D
CMSC 201 – Lab 3 Strings
Overview • Objectives for today's lab: • Obtain experience using strings in Python, including looping over characters in strings and conversion from ASCII • Create a program that uses string operations to generate random passwords based on some of the password rules used at UMBC
Simplified UMBC Password Rules May only contain printable characters (alphanumeric and symbols -- no spaces or "control" characters) Must be at least 8 characters in length Must include at least one upper case, lower case, and one non-alphabetic character Must not contain three or more recurring characters. (such as 'sss')
Program Outline • Required Code • Implement Rules 1-2 • Print the new random password • Bonus Code • Implement Rule 3 • Challenge • Implement Rule 4
Required 0: Setup The first step is to create a lab3 folder in your cs201/labs directory From your home directory: cd 201/labs mkdir lab3 cd lab3 emacs lab3.py &
Required 0: Setup Edit the lab3.py to look like the following: # File: lab3.py # Written by: YOUR NAME HERE # Date: DATE HERE # Section: ? # Email: ?@umbc.edu # Description: Lab 3, Random Password Generator import random def main(): #put code here main()
Required 0: ASCII Codes • First, you need to familiarize yourself with the ASCII codes. • http://www.ascii.cl/ • Ignore the column called Hex – use the column called ASCII • There is a 1-to-1 relationship between an ASCII code and the letter it represents • Find which codes are printable (valid chars) • Find which codes are numeric chars • Find which codes are special chars • Find which codes are lower case chars • Find which codes are upper case chars
Required 1: Pseudocode create a new variable to hold the password loop 8 times generate random number in the printable char range save it into a variable turn the number into a character add the new char to the end of the password string print new password
Required 2: Compututations • Generate a random ASCII numeric code • random.randrange(a, b) will generate and return a random, integer value in the range a - b • Assign the value that was returned to a new variable • Convert the random number to a character value • chr( variable) takes an integer value and returns a character • Use the random number from above in the chr( ) function • Assign the value that was returned to a new variable • Add the new char to the end of the password • Use + to concatenate • string_var = string_var + new_character_var
Required: Output • Print the new password travis-laptop-2% python lab3.py $d5iDmK#
Bonus: Description • Bonus Code Rule 3: Password must include at least one upper case, lower case, and one non-alphabetic character • Change the for-loop into a while-loop • Continue to add characters to the password until there are enough of the right kinds of characters (with a minimum of 8)
Bonus: Psuedocode set counter variables to 0 set done condition variable to false loop while done is false generate random number turn number into a character and add to password increment counts of the different types of chars if all the counts fulfill rules 1-3 set done condition variable to true print new password
Bonus: Initialization • Create variables to keep track of how many characters there are of each type. • You will need variables for uppercase, lowercase, and non-alphabetic • Make sure to give them appropriate names • Start counting at 0 • Create a boolean variable to flag whether the password fulfills all the rules • The flag will be false initially
Bonus: while loops Loop while a condition is true set condition to true # execute stmts in loop while a condition is true while (condition is true): # execute 1 or more statements # at some point, make condition false set condition to false
Bonus: Computations • Loop until the password fulfills all the rules • Generate a random printable character • Increment the counter for the type of character that it is • Add the character to the password • If the password fulfills all the rules, set the flag to true
Challenge • Implement Rule 4 • Must not contain three or more recurring characters • such as 'sss'