Activity 2-6

March 14, 2012

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. Use the sort() function, which is a member function of a list. When you call myList.sort(), remember that it changes myList! Try sorting a short list of strings.
  4. Add the key 'george' (lowercase 'g'!) with the password of your choice as the value. Run printDictionary again. How is the list sorted?
  5. Replace the key-value pair whose key is 'george' with one whose key is 'George', with the same value. You actually need to do two things to passwordDictionary to achieve this.

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.