Use the command line to create a new directory called lab25 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.
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:
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 |
Example
>>> 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.
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, 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:
-
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 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.
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 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.
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
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.