Homework 2-5

Adapted from Brown University CSCI0931. Used with Permission.

Task 1: Python Dictionary Review

Download hw2-5.py and save it as Name_StudentID_HW2-5.py. 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 2014. First, make sure your password doesn't appear on this list: if it does, you should probably go and change it!
  2. Run the function printDictionary with passwordDictionary as input. Make sure you understand how this function works.
  3. Modify printDictionary so it prints the keys in alphabetical order. We need to sort the list of keys alphabetically. There are two choices:
    • Use the function sorted(), which takes in a list and returns a sorted version of it. We used this in HW 2-4 when we did sortedList = sorted(myList) to get the fast version of removeDuplicates.
    • Use the function sort(), which is called on a list. For example, myList.sort(). It doesn't return anything, but after calling it on a list, that list will be sorted.
    Try both of these functions. In IDLE, define a list myList = ['my', 'name', 'is', 'ChanTaiMan']. The try using sortedList = sorted(myList) and print out the contents of sortedList and myList. Then try myList.sort() and print out the contents of myList. What is the difference between these two methods? Write your explanation in the comments for printDictionary. Then pick one method and use it in your modification of printDictionary.

Task 2: Write an addPassword Function

For this next task, we will write a function that will allow the user to add a new name and password to the dictionary. In IDLE, run the echo function (you can do that by just typing echo() at the >>> IDLE prompt.)

  1. What does the built-in input function do? Write your answer in the comments to echo
  2. The addUser function is supposed to add a user to the dictionary. It should use input() to get the input from the user, and then call addPassword to add the password. An example run should look like:
    Enter the name of the user: grace
    Enter password: 123456
    Password and name successfully entered!
  3. 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. Build in a security feature to stop random people from resetting other people's passwords by pretending to add new users. 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 warning message and do not modify the dictionary.

  4. The updateUserName allows us to change the name of a user (for example, if he/she typed it in wrong before, or if somebody gives him/herself a new English name.) A sample run of the program should look like the following:
    Which user's name do you want to update? Uma
    What is the the new name? Una
    Type in the password for Uma: qazwsx
    "Uma" has successfully been corrected to "Una"
    As you can see, updateUserName should have some built-in security feature that will require the user to type in his/her correct password before the name can be updated. To practice code reuse, the function should call addPassword to insert the new user name and password (i.e. if two bits of code do very similar things, they should be extracted into a function, and we can just call that function instead of retyping that code).
  5. Note: updateUserName should also have a security feature that stops the user from picking a name that somebody else is already using! For example:
    Which user's name do you want to update? Uma
    What is the the new name? Valerie
    Sorry! Valerie is already taken!
    Test your function with different values of current and updated user names to make sure that it works properly!


Rename your program Name_StudentID_HW2-5.py and share it with PolyUCOMP1D04@gmail.com.

Note: Before you turn in your Python files, make sure they run without any errors(Save your Python file. Then select Run > Run Module or hit F5 on your keyboard)! If nothing appears in the Shell, don't worry as long as no red error messages appear. If they don't run, i.e. if red stuff starts appearing in the shell, points will be taken off!