Lab 05
from datetime import datetime
now = datetime.now()
print(f"Submitted time: {now}")
Submitted time: 2024-07-21 14:43:32.132742
Question 1¶
Person: Use a dictionary to store information about a person you know. Store their first name, last name, age, and the city in which they live. You should have keys such as first_name, last_name, age, and city. Print each piece of information stored in your dictionary.
Question 2¶
Favorite Numbers: Use a dictionary to store people’s favorite numbers. Think of five names, and use them as keys in your dictionary. Think of a favorite number for each person, and store each as a value in your dictionary. Print each person’s name and their favorite number. For even more fun, poll a few friends and get some actual data for your program.
Question 3¶
Glossary: A Python dictionary can be used to model an actual dictionary. However, to avoid confusion, let’s call it a glossary.
- Think of five programming words you’ve learned about in the previous chapters. Use these words as the keys in your glossary, and store their meanings as values.
- Print each word and its meaning as neatly formatted output. You might print the word followed by a colon and then its meaning, or print the word on one line and then print its meaning indented on a second line. Use the newline character (\n) to insert a blank line between each word-meaning pair in your output.
Question 4¶
Glossary 2: Now that you know how to loop through a dictionary, clean up the code from Question 3 by replacing your series of print() calls with a loop that runs through the dictionary’s keys and values. When you’re sure that your loop works, add five more Python terms to your glossary. When you run your program again, these new words and meanings should automatically be included in the output.
Question 5¶
Rivers: Make a dictionary containing three major rivers and the country each river runs through. One key-value pair might be 'nile': 'egypt'.
- Use a loop to print a sentence about each river, such as The Nile runs through Egypt.
- Use a loop to print the name of each river included in the dictionary.
- Use a loop to print the name of each country included in the dictionary.
Question 6¶
Polling: Based on the dictionary below (i.e., favorite_languages):
- Make a list of people who should take the favorite languages poll. Include some names that are already in the dictionary and some that are not.
- Loop through the list of people who should take the poll. If they have already taken the poll, print a message thanking them for responding. If they have not yet taken the poll, print a message inviting them to take the poll.
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
Question 7¶
People: Start with the program you wrote for Question 1. Make two new dictionaries representing different people, and store all three dictionaries in a list called people
. Loop through your list of people. As you loop through the list, print everything you know about each person.
Question 8¶
Pets: Make several dictionaries, where each dictionary represents a different pet. In each dictionary, include the kind of animal and the owner’s name. Store these dictionaries in a list called pets
. Next, loop through your list and as you do, print everything you know about each pet.
Question 9¶
Favorite Places: Make a dictionary called favorite_places
. Think of three names to use as keys in the dictionary, and store one to three favorite places for each person. To make this exercise a bit more interesting, ask some friends to name a few of their favorite places. Loop through the dictionary, and print each person’s name and their favorite places.
Question 10¶
Favorite Numbers: Modify your program from Question 2 so each person can have more than one favorite number. Then print each person’s name along with their favorite numbers.
Question 11¶
Cities: Make a dictionary called cities
. Use the names of three cities as keys in your dictionary. Create a dictionary of information about each city and include the country that the city is in, its approximate population, and one fact about that city. The keys for each city’s dictionary should be something like country
, population
, and fact
. Print the name of each city and all of the information you have stored about it.
Question 12¶
Extensions: We’re now working with examples that are complex enough that they can be extended in any number of ways. Use one of the example programs from this chapter, and extend it by adding new keys and values, changing the context of the program or improving the formatting of the output.
Question 13¶
Rental Car: Write a program that asks the user what kind of rental car they would like. Print a message about that car, such as “Let me see if I can find you a Subaru.”
Question 14¶
Restaurant Seating: Write a program that asks the user how many people are in their dinner group. If the answer is more than eight, print a message saying they’ll have to wait for a table. Otherwise, report that their table is ready.
Question 15¶
Multiples of Ten: Ask the user for a number, and then report whether the number is a multiple of 10 or not.
Question 16¶
Pizza Toppings: Write a loop that prompts the user to enter a series of pizza toppings until they enter a 'quit' value. As they enter each topping, print a message saying you’ll add that topping to their pizza.
Question 17¶
Movie Tickets: A movie theater charges different ticket prices depending on a person’s age. If a person is under the age of 3, the ticket is free; if they are between 3 and 12, the ticket is \$10; and if they are over age 12, the ticket is $15. Write a loop in which you ask users their age, and then tell them the cost of their movie ticket.
Question 18¶
Three Exits: Write different versions of either Question 16 or Question 17 that do each of the following at least once:
- Use a conditional test in the while statement to stop the loop.
- Use an active variable to control how long the loop runs.
- Use a break statement to exit the loop when the user enters a 'quit' value.
Question 19¶
Deli: Make a list called sandwich_orders
and fill it with the names of various sandwiches. Then make an empty list called finished_sandwiches
. Loop through the list of sandwich orders and print a message for each order, such as I made your tuna sandwich
. As each sandwich is made, move it to the list of finished sandwiches. After all the sandwiches have been made, print a message listing each sandwich that was made.
Question 20¶
No Pastrami: Using the list sandwich_orders
from Question 19, make sure the sandwich pastrami
appears in the list at least three times. Add code near the beginning of your program to print a message saying the deli has run out of pastrami, and then use a while
loop to remove all occurrences of pastrami
from sandwich_orders
. Make sure no pastrami sandwiches end up in finished_sandwiches
.
Question 21¶
Dream Vacation: Write a program that polls users about their dream vacation. Write a prompt similar to If you could visit one place in the world, where would you go? Include a block of code that prints the results of the poll.