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.
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.
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:
- 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 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:
-
Your quiz must consist of AT LEAST 5 questions.
-
Your quiz must be entirely stored in a dictionary.
-
When the user answers a question, you must indicate if they were correct or incorrect.
-
If the user was incorrect, you should indicate what the correct answer was.
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
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:
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 |
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 inmy_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!
Submission
Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.