Build Dictionary
Write the function build_dictionary(key1, value1, key2,
value2, key3, value3)
that creates a dictionary from 3
keys and 3 valus. The parameters key1, key2,
and key3 are immutable objects and the
parameters value1, value2, and value3
are any value. The function should return a new dictionary that
contains the specified keys with their respective values. That
is, key_1 is associated with value_1
Test Cases
print('Input: \'a\', 1, \'b\', 2, \'c\', 3\tActual: ', build_dictionary('a', 1, 'b', 2, 'c', 3), '\tExpected: {\'a\': 1, \'b\': 2, \'c\': 3}') print('Input: 1, \'a\', 2, \'b\', 3, \'c\'\tActual: ', build_dictionary(1, 'a', 2, 'b', 3, 'c'), '\tExpected: {1: \'a\', 2: \'b\', 3: \'c\'}')
Get Value
Write the function get_value(dictionary, key)
that
returns the value associated with a key in a dictionary. The
parameter dictionary is a dictionary object
and key is an immutable value. The function should
return the value associated with key
in dictionary. If there is no value associated
with key the function should return None.
Test Cases
print('Input: {\'a\': 1, \'b\': 2, \'c\': 3}, \'a\'\tActual: ', get_value({'a': 1, 'b': 2, 'c': 3}, 'a'), '\tExpected: 1') print('Input: {\'a\', 1: \'b\', 2: \'c\', 3}, \'d\'\tActual: ', get_value({'a': 1, 'b': 2, 'c': 3}, 'd'), '\tExpected: None')
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
Write the function translate(text)
. The function
should return a string that is equivalent to the string
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:
ABBREV | TXT | MSG | U | LUV | UR | GR8 | 2MORO | 2DAY | 2NITE | CU | B4 | THNQ |
---|---|---|---|---|---|---|---|---|---|---|---|---|
English | text | message | you | love | you are | great | tomorrow | today | tonight | see you | before | thank you |
Test Cases
print('Input: "GR8 I\'ll CU 2NITE"\tActual:', translate("GR8 I'll CU 2NITE"), '\tExpected: great I\'ll see you tonight') print('Input: "I LUV U"\tActual:', translate("I LUV U"), '\tExpected: I love you')
Hint
-
Initialize a 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!"
-
Use
the
split
method to create a list of words in the input string. -
Check to see if a word is a key in the dictionary using
the
in
operator. If the string "hi" is a key inmy_dict
, then"hi" in my_dict
returns True. - Use the accumulator to create the translated string. 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.
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. Instead of taking a quiz you are going to create a quiz application. Your quiz can be on whatever you want it to be. The choice is yours. Some ideas:
- U.S. State Capitals:
Matching a state capital to the state it belongs.
- Sports Team Cities:
Matching a specified sports franchise to their home city.
-
Album Artist:
Match an album to the original artist.
-
Video Game Consoles:
Matching a specified video game to the console it is commonly associated.
Details
Create a program that prompts 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, the program should indicate how many questions they got correct.
This exercise is pretty open ended, but there are a few restrictions:
- Your quiz must consist of AT LEAST 5 questions.
- Your quiz must be entirely stored in a dictionary.
- When the user answers a question, the program must print whether the answer was correct or incorrect.
- If the user is incorrect, the program should print the correct answer.
Example
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
- The key for the dictionary should be the question being asking, and the value for the dictionary should be the answer to the question.
-
Create a loop that iterates over the keys of the dictionary.
Get a view of the questions using the dictionary
keys
method.
Challenge
If you ask any student on campus what their favorite type of test is, the answer 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. Make a multiple choice quiz.
Which state's capital is Richmond? a) Virginia b) Missouri c) Oregon d) Utah Answer: d Wrong! The answer was a) Virginia