Activity: Dictionaries and Pirates

Create a new python file called pirates.py and save to your cs100/ch4 folder.

  1. Write Python code that illustrates all the methods mentioned in our code notes. You may use a new dictionary created by yourself or the following one:
    cents = { 'Nickel': 5, 'Dime': 10, 'Quarter': 25, 'Dollar': 100}
    

    For example, iterate through the dictionary by key and print out all key/value pairs. Next, use the keys() function to obtain all keys from your dictionary and then print it out to test it. Make sure you are printing the results of the function calls / operations used on your dictionary to test how it works.

  2. Use the dictionary below in a program that (a) asks the user to input an integer bonus which you save to an integer variable, and then (b) adds the bonus to the salary of all employees in your dictionary, and then (c) prints out each person’s salary. (Hint: Use a for loop to update the salary by the bonus amount.)
    personnel = {'Joe': 2000, 'Ana': 2500, 'Bob': 1800, 'Chris': 2100, 'Diana': 1900}
    
  3. Have you ever tried to talk like a pirate? Write a function, toPirate(sentence), that takes an English sentence in the form of a string as a parameter. The toPirate function should return a string containing the pirate translation of your sentence. Use the table below to construct a translation dictionary. You can also consult other sources online for more pirate phrases.
English Pirate Translation
Greetings hello
excuse
avast
arr
People sir, boy, man
madam
officer
matey
proud beauty
foul blaggart
Articles the
my
your
is
are
th'
me
yer
be
be
Places restroom
restaurant
hotel
head
galley
fleabag inn

Test your function with a few different inputs. For example:

   sentence = "hello there madam how are you"
   print( toPirate(sentence) ) # will print "avast there proud beauty how be you"
  1. Write a function combineDictionaries(d1, d2) that takes two dictionaries as parameters and returns a new dictionary that includes the items from both. Test your function with:
    nhl1 = {
        "Toronto": "Maple Leafs",
        "Montreal": "Canadiens",
        "Vancouver": "Canucks",
        "Boston": "Bruins",
        "Edmonton": "Oilers",
        "New York": "Rangers"
        }
       
    nhl2 = {
        "Detroit": "Red Wings",
        "Pittsburgh": "Penguins",
        "Philedelphia": "Flyers",
        "Calgary": "Flames",
        "Tampa Bay": "Lightning"
        }
    print( combineDictionaries(nhl1, nhl2) )
    

If you finish early

  1. Write Python code (no function need) to create a dictionary named userPasswords which holds 3 usernames (as keys) and their corresponding passwords (as values). Your program should allow for the user to input these values. Use a loop to ask the user for input for the username and for the password. Print the dictionary after all username/passwords have been added.

  2. Add code that asks the user to input their username. Your program will print their password, or “User not found” if the username doesn’t exist. This simulates that a user forgot their password.

  3. Add code that asks the user to input their username and then their password. Check if the passwords match.

  4. Challenging: Create a new dictionary called protectedUserPasswords which asks the user to type in 3 username/password pairs.This time, store the password as encrypted text rather than plain text. Next, add code so that the user can input their username/password and check for a match. Hint: think about how to match a password to an encrypted password.

How to submit

Submit your working python file to Moodle.