1 / 16

CMSC 201 – Lab 3

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

dolph
Download Presentation

CMSC 201 – Lab 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 201 – Lab 3 Strings

  2. 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

  3. 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')

  4. Program Outline • Required Code • Implement Rules 1-2 • Print the new random password • Bonus Code • Implement Rule 3 • Challenge • Implement Rule 4

  5. 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 &

  6. 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()

  7. 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

  8. 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

  9. 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

  10. Required: Output • Print the new password travis-laptop-2% python lab3.py $d5iDmK#

  11. 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)

  12. 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

  13. 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

  14. 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

  15. 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

  16. Challenge • Implement Rule 4 • Must not contain three or more recurring characters • such as 'sss'

More Related