Download and save ACT2-6.py
. Open it in IDLE and press F5.
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!printDictionary
with passwordDictionary
as input. Make sure you understand how this function works (it is similar to a function from last class).printDictionary
so it prints the keys in alphabetical order. We need to sort the list of keys alphabetically. There are a couple choices:
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
.sort()
is called on a list object. For example, myList.sort()
. It doesn't return anything, but after calling it on a list, that list will be sorted afterwards.myList = ['my', 'name', 'is', 'Steve']
and try out both these functions starting with sorted()
. When you finally call myList.sort()
, remember that it changes myList
!'george'
(lowercase 'g'!) with the password of your choice as the value. Run printDictionary
again. How is the list sorted?'george'
with one whose key is 'George'
, with the same value. You actually need to do two things to passwordDictionary
to achieve this.addPassword
FunctionThe 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.
addPassword
function to print a statement that says 'Warning, overwriting an existing password!'
if you overwrite a password.'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.In ACT2-6.py
, 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.
foundPosition = mobyString.find('November')
. What do you think this find()
function returns? Type print(mobyString[foundPosition:len(mobyString)])
. What does it print?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?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)).
mobyString.replace('November', 'December')
. Look at the output. What did it do?mobyString.replace('I', 'YOUR-LOYAL-CS931-TEACHER', 6)
. What do you notice about where and how many times 'I' gets replaced?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?spacey.lstrip(' foo')
and look at the output.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?