This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9-4. Number Served: Start with your program from Exercise 9-1 (page 162). | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9-1. 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. | |
9-2. Three Restaurants: Start with your class from Exercise 9-1. Create three | |
different instances from the class, and call describe_restaurant() for each |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8-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. | |
8-13. User Profile: Start with a copy of user_profile.py from page 149. Build a | |
profile of yourself by calling build_profile(), using your first and last names | |
and three other key-value pairs that describe you. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8-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. | |
8-10. Sending Messages: Start with a copy of your program from Exercise 8-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. | |
8.9 Answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8-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. | |
8-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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8-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. | |
8-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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8-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. | |
8-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. | |
8.1 Answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7-8. 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. | |
7-9. No Pastrami: Using the list sandwich_orders from Exercise 7-8, 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7-7. Infinity: Write a loop that never ends, and run it. (To end the loop, press | |
ctrl-C or close the window displaying the output.) | |
7.7 Answer | |
while True: | |
age = input("How old are you? ") | |
age = int(age) | |
if age < 3: | |
print("Your ticket is free") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7-4. 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. | |
7-5. 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. | |
NewerOlder