< Back

Lab 25: Dictionaries

As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.

    $ cd ~/cs120/labs
    $ mkdir lab25 
    $ cd lab25 


Practice 1

Write a function called build_dictionary(key_1, value_1, key_2, value_2, key_3, value_3), which takes 6 values as parameters. You are assured that key_1, key_2, and key_3 are immutable objects. You should build (and return) a dictionary which contains the specified keys with their respective values.



Practice 2

Write a function called get_value(a_dictionary, key), which takes a dictionary and some immutable data as a parameter. Your function should return whatever value is associated with the given key in the given dictionary.



TXT MSG

OMG, UR GMOM TOTES H8s TXT MSGs W/ ABBREVs. Create a program to help your grandmother translate text messages with abbreviations to plain English.

Details

Create a function called translate_txt(text) in a file called txt.py. The function should return a string that is equivalent to the parameter text, with all abbreviations replaced with complete words or phrases. Assume that there is no punctuation in the message and that all words are separated with a space character. Do not use the string replace method. The program should use a dictionary that contains the following abbreviations:

ABBREVTXTMSG ULUV URGR8 2MORO2DAY 2NITECU B4THNQ
Englishtextmessage youlove you aregreat tomorrowtoday tonightsee you beforethank you

Sample Test Cases

Function Parameters Expected Output
"GR8 I'll CU 2NITE" great I'll see you tonight

Hint

  • You will need to initialize the dictionary as a global variable using the curly brace notation. Recall that you can use the curly brace notation as follows:
    	  my_dictionary = {"CS": "FUN!", "Math": "Yay!"}
    	  print(my_dictionary["CS"]) # "FUN!"
    	  print(my_dictionary["Math"]) # "Yay!"
    	
  • You need to use the split method to create a list of words in the input string.

  • You can check to see if a word is a key in your dictionary using the in operator. If the string "hi" is a key in my_dict, then "hi" in my_dict returns True.

  • You are going to use the accumulator pattern for this activity. If the word is in the dictionary, add the translation. If it is not in the dictionary, add just the word.

 

Challenge

It's important to be polite to your elders, but sometimes generational language barriers can make it difficult for your grandmother to see how polite you actually are. Extend grandma's text message app to translate to geriatric. The function should be able to translate phases consisting of multiple words.

>>> print(translate_txt("So long and thanks for all the fish!"))
Good bye, it was really great seeing you and I sincerely appreciate all the fish!

Pop-quiz!

Just kidding. Well… not quite kidding. You see, Scotty's INQ class is getting tired of his antics, and would much rather take quizzes instead.

The problem is that Scotty can be lazy on occasion, and this is one of those occasions. He has tasked you with coming up with a program that would allow his INQ students to take an interactive quiz.

The INQ students don't much care what the quiz is, so your quiz can be on whatever you want it to be. The choice is yours. Some ideas:

Details

Create a program in a file called pop_quiz.py. Your program should ask the user to answer a series of questions. The user should be allowed to type in their answer to the question, short-answer style. After the user answers all of the questions, you should indicate how many questions they got correct.

This exercise is pretty open ended, but there are a few restrictions:

Example

$ python3 pop_quiz.py
Welcome to the State Capital Quiz!
/////////////////\\\\\\\\\\\\\\\\\
Which state's capital is Richmond? Virginia
Correct!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Which state's capital is Montpelier? Ontario
Wrong! The answer was Vermont.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.
.
.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You answered 3 out of 5 questions correctly.

Hint

  • Your key for the dictionary should be the question you are asking, and the value for the dictionary should be answer to the question.

  • Create a function ask_question(question, answer). This function takes a question and answer as parameters, both strings. This function should return True If the user gets the question right, False otherwise.

  • You will have to create a loop that iterates over the keys of your dictionary. You can get a list of your questions using the keys method of dictionaries.

 

Challenge

If you ask any student on campus what their favorite type of test is, the answer is will almost universally be "Multiple Choice." Most student's deductive reasoning skills grant them the capability to make very educated guesses, resulting in the perception of an easier test.

Dr. Bouchard's students want as easy of a quiz as possible. While none of the above ideas necessarily benefit from being multiple choice, his students would likely appreciate your altering of the short-answer quiz to one of a multiple choice variety.

$ python3 pop_quiz.py
Which state's capital is Richmond?
  a) Virginia
  b) Missouri
  c) Oregon
  d) Utah
Answer: d
Wrong!  The answer was a) Virginia