CS 100

Logo

Scientific Computing

Activity 18

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

  1. Use the dictionary below in a program that (a) asks the user to input a bonus, 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.)
    personnel = {'Joe': 2000, 'Ana': 2500, 'Bob': 1800, 'Chris': 2100, 'Diana': 1900}
    
  2. 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 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.

If you finish early

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

  2. 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.

  3. 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) )
    

How to submit

Submit your working python file to Moodle.