Lab 06
from datetime import datetime
now = datetime.now()
print(f"Submitted time: {now}")
Submitted time: 2024-07-21 14:43:33.572801
Question 1¶
Message: Write a function called display_message()
that prints one sentence telling everyone what you are learning about in this chapter. Call the function, and make sure the message displays correctly.
Question 2¶
Favorite Book: Write a function called favorite_book()
that accepts one parameter, title. The function should print a message, such as One of my favorite books is Alice in Wonderland
. Call the function, making sure to include a book title as an argument in the function call.
Question 3¶
T-Shirt: Write a function called make_shirt()
that accepts a size and the text of a message that should be printed on the shirt. The function should print a sentence summarizing the size of the shirt and the message printed on it.
Call the function once using positional arguments to make a shirt. Call the function a second time using keyword arguments.
Question 4¶
Large Shirts: Modify the make_shirt()
function so that shirts are large by default with a message that reads I love Python. Make a large shirt and a medium shirt with the default message, and a shirt of any size with a different message.
Question 5¶
Cities: Write a function called describe_city()
that accepts the name of a city and its country. The function should print a simple sentence, such as Reykjavik is in Iceland
. Give the parameter for the country a default value. Call your function for three different cities, at least one of which is not in the default country.
Question 6¶
City Names: Write a function called city_country()
that takes in the name of a city and its country. The function should return a string formatted like this:
Santiago, Chile
Call your function with at least three city-country pairs, and print the values that are returned.
Question 7¶
Album: Write a function called make_album()
that builds a dictionary describing a music album. The function should take in an artist name and an album title, and it should return a dictionary containing these two pieces of information. Use the function to make three dictionaries representing different albums. Print each return value to show that the dictionaries are storing the album information correctly.
Use None to add an optional parameter to make_album() that allows you to store the number of songs on an album. If the calling line includes a value for the number of songs, add that value to the album’s dictionary. Make at least one new function call that includes the number of songs on an album.
Question 8¶
User Albums: Start with your program from Question 7. Write a while
loop that allows users to enter an album’s artist and title. Once you have that information, call make_album()
with the user’s input and print the dictionary that’s created. Be sure to include a quit value in the while
loop.
Question 9¶
Messages: Make a list containing a series of short text messages. Pass the list to a function called show_messages()
, which prints each text message.
Question 10¶
Sending Messages: Start with a copy of your program from Question 9. Write a function called send_messages()
that prints each text message and moves each message to a new list called sent_messages
as it’s printed. After calling the function, print both of your lists to make sure the messages were moved correctly.
Question 11¶
Archived Messages: Start with your work from Question 10. Call the function send_messages()
with a copy of the list of messages. After calling the function, print both of your lists to show that the original list has retained its messages.
Question 12¶
Sandwiches: Write a function that accepts a list of items a person wants on a sandwich. The function should have one parameter that collects as many items as the function call provides, and it should print a summary of the sandwich that’s being ordered. Call the function three times, using a different number of arguments each time.
Question 13¶
Cars: Write a function that stores information about a car in a dictionary. The function should always receive a manufacturer and a model name. It should then accept an arbitrary number of keyword arguments. Call the function with the required information and two other name-value pairs, such as a color or an optional feature. Your function should work for a call like this one:
car = make_car('subaru', 'outback', color='blue', tow_package=True)
Print the dictionary that’s returned to make sure all the information was stored correctly.
Question 14¶
Restaurant: Make a class called Restaurant
. The __init__()
method for Restaurant
should store two attributes: a restaurant_name
and a cuisine_type
. Make a method called describe_restaurant()
that prints these two pieces of information, and a method called open_restaurant()
that prints a message indicating that the restaurant is open.
Make an instance called restaurant
from your class. Print the two attributes individually, and then call both methods.
Question 15¶
Three Restaurants: Start with your class from Question 14. Create three different instances from the class, and call describe_restaurant()
for each instance.
Question 16¶
Users: Make a class called User
. Create two attributes called first_name
and last_name
, and then create several other attributes that are typically stored in a user profile. Make a method called describe_user()
that prints a summary of the user’s information. Make another method called greet_user()
that prints a personalized greeting to the user.
Create several instances representing different users, and call both methods for each user.
Question 17¶
Number Served: Start with your program from Question 14. Add an attribute called number_served
with a default value of 0. Create an instance called restaurant
from this class. Print the number of customers the restaurant has served, and then change this value and print it again.
Add a method called set_number_served()
that lets you set the number of customers that have been served. Call this method with a new number and print the value again.
Add a method called increment_number_served()
that lets you increment the number of customers who’ve been served.
Call this method with any number you like that could represent how many customers were served in, say, a day of business.
Question 18¶
Login Attempts: Add an attribute called login_attempts to your User class from Question 16. Write a method called increment_login_attempts()
that increments the value of login_attempts
by 1. Write another method called reset_login_attempts()
that resets the value of login_attempts to 0.
Make an instance of the User
class and call increment_login_attempts()
several times. Print the value of login_attempts
to make sure it was incremented properly, and then call reset_login_attempts()
. Print login_attempts
again to make sure it was reset to 0.
Question 19¶
Dice: Make a class Die
with one attribute called sides
, which has a default value of 6. Write a method called roll_die()
that prints a random number between 1 and the number of sides the die has. Make a 6-sided die and roll it 10 times.
Make a 10-sided die and a 20-sided die. Roll each die 10 times.
Question 20¶
Lottery: Make a list or tuple containing a series of 10 numbers and five letters. Randomly select four numbers or letters from the list and print a message saying that any ticket matching these four numbers or letters wins a prize.
You can use a loop to see how hard it might be to win the kind of lottery you just modeled. Make a list or tuple called my_ticket
. Write a loop that keeps pulling numbers until your ticket wins. Print a message reporting how many times the loop had to run to give you a winning ticket.