As usual, create two directories for today's class. Create a
directory called lecture30
under activities, and
a directory called lab30
under labs.
When using lists, we can only use the integer data-type to refer to a particular location within the list. A majority of the time, this is O.K., however sometimes we want to use some other data-type, like strings. Today, you will learn about Dictionaries, which are essentially Associative lists — a list that associates some immutable data-type to a value.
We are going to try something new today. There is a Code Lab activity to do in class today. You should try these exercises on your own first, then discuss your solutions with your lab partner.
OMG, UR GMOM TOTES H8s TXT MSGs W/ ABBREVs. Create a program to help your grandmother translate text messages with abbreviations to plain English.
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 |
>>> print(translate_txt("GR8 I'll CU 2NITE")) great I'll see you tonight
Initialze the dictionary as a global variable using the curly brace
notation. In the translate_txt
function, use the
string split
method to create a list of words in the input string. Then iterate
over the list with a while loop and check if each word is a key in the
dictionary using the in
operator. If the word is a
key in the dictionary,
replace the word in the list with the result of looking the word up in
the dictionary. Finally, turn the list back into a string using the
string join
method.
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!
Just kidding. Well… not quite kidding. You see, Dr. Bouchard's INQ class is getting tired of his lectures, and would much rather take quizzes as opposed to learning actual useful information.
The problem is that Dr. Bouchard 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:
Matching a state capital to the state it belongs.
Matching a specified sports franchise to their home city.
Match an album to the original artist.
Matching a specified video game to the console it is commonly associated.
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 8 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.
$ 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 6 out of 8 questions correctly.
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.
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
When you have finished, create a tar file of your lab30
directory. To create a tar file, execute the following commands:
cd ~/cs120/labs tar czvf lab30.tgz lab30/
To submit your activity, go to cseval.roanoke.edu. You should
see an available assignment called Lab Assignment 30
. Only
one of your pair should submit your activity. Make sure both partners
are listed in the header of your files.
Do not forget to email your partner today's files!