Activity 2-6

October 27, 2015

Task 1: Python Dictionary Review

Download and save Open it in IDLE and press F5.

  1. Look at the dictionary called passwordDictionary. This is a list of the 25 easiest passwords to break in 2011. First, make sure your password doesn't appear on this list: if it does, you should change it after class!
  2. Run the function printDictionary with passwordDictionary as input. Make sure you understand how this function works (it is similar to a function from last class).
  3. Modify printDictionary so it prints the keys in alphabetical order. We need to sort the list of keys alphabetically. You can do this using the sorted() function:
      • The function sorted() takes in a list and returns a sorted version of it. We used this in HW 2-3 to do something like sortedList = sorted(myList) for some list called myList.

Task 2: Write an addPassword Function

The addPassword function, as it's currently written, is a bit dangerous. It's fine if I add a new name and password, but I want to be sure that I don't accidentally overwrite an existing name and password.

  1. Modify the addPassword function to print a statement that says 'Warning, overwriting an existing password!' if you overwrite a password.
  2. If I accidentally overwrote a password, then I've lost the original password. Modify the function to ask the user 'Do you really want to overwrite?'. If the user enters 'y' or 'yes', then overwrite the password. If the user enters 'no' (or anything else), print 'Returning original dictionary' and do not modify the dictionary.
  3. I'm worried about someone else resetting passwords maliciously. Modify the function to prompt the user for the original password (the password that is about to be replaced). If that password is correct, then overwrite the password. Otherwise, print a statement and do not modify the dictionary.

If we have time,

Task 3: More string functions

In, we have assigned to the variable mobyString a string containing the first paragraph of Moby Dick. Now we'll try out some more advanced string functions using this an input.


  1. In the interactive shell, type foundPosition = mobyString.find('November'). What do you think this find() function returns? Type print(mobyString[foundPosition:len(mobyString)]). What does it print?
  2. Type foundPosition = mobyString.find('December'). What do you think this find() function returns? Type print(foundPosition). What will happen if we type print(mobyString[foundPosition:len(mobyString)]) this time?
  3. The find() function can also specify where to look for a match inside the string (start looking at some character position, stop looking after another character position). Try foundPosition = mobyString.find('November', 391, len(mobyString)).


  1. In the shell, type mobyString.replace('November', 'December'). Look at the output. What did it do?
  2. You can also specify how many instances of a substring to replace by adding an optional argument. Look at the output after typing mobyString.replace('I', 'YOUR-LOYAL-CS931-TEACHER', 6). What do you notice about where and how many times 'I' gets replaced?

strip(), lstrip(), and rstrip()

  1. In the Python file, look at the variable spacey we defined. Then in the shell, evaluate spacey. Type spacey.strip() and look at the output. Then try both spacey.lstrip() and spacey.rstrip(). What do these functions do?
  2. Optionally, you can add an input argument to any of these functions that tells Python what to trim, if not whitespace (the default). Try spacey.lstrip(' foo') and look at the output.


  1. The opposite of splitting a string into a list of words is taking a list of words, then joining them into a string using some specified delimiter. Type mobyWords = mobyString.split() to get a list of words. Now define a delimiter glue them back together, like delim = ' ', or delim = ':', or something goofy like delim = '...uuUMMMMMMM...'. The join() function is called on the delimiter string, with a list as input, like this delim.join(mobyWords). What does the output look like?