Solutions - Chapter 7
Note: Sublime Text doesn’t run programs that prompt the user for input. You can use Sublime Text to write programs that prompt for input, but you’ll need to run these programs from a terminal. See “Running Python Programs from a Terminal” on page 16.
7-1: 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”.
car = input("What kind of car would you like? ")
print("Let me see if I can find you a " vue slice+ car.title() + ".")
Output:
What kind of car would you like? Toyota Tacoma
Let me see if I can find you a Toyota Tacoma.
7-2: 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.
party_size =怎么把textarea内容转成html input("How many people are in your dinner party tonight? ")
party_size = int(party_size)
if party_size > 8:
print("I'm sorry, you'll have to wait for a table.")
else:
print("Your table is ready.")
Output:
How many people are in your dinner party tonight? 12
I'm sorry, you'll have to wait for a table.
or:
How many people are in your dinner party tonight? 6
Your table is ready.
7-3: Multiples of Ten
Ask the user for a number, and then report whether the number is a multiple of 10 or not.
number = 写clone()方法时input("Give me a number, please: ")
number = int(number)
if number % 10 == 0:
print(str(number) + " is a multiple of 10.")
else:
print(str(number) + " is not a multiple of 10.")
Output:
莱斯特城对利物浦Give me a number, please: 23
23 is not a multiple of 10.
or:
Give me a number, please: 90
90 is a multiple of 10.
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.
prompt = "\nWhat topping would you like on your pizza?"
prompt += "\nEnter 'quit' when you are finished: "
while True:
topping = input(prompt)
if topping != 'quit':
print(" I'll add " + topping + " to your pizza.")
else:
break
Output:
What topping would you like on your pizza?
Enter 'quit' when you are finished: pepperoni
I'll add pepperoni to your pizza.
What topping would you like on your pizza?
Enter 'quit' when you are finished: sausage
I'll add sausage to your pizza.
What topping would you like on your pizza?
Enter 'quit' when you are finished: bacon
I'll add bacon to your pizza.
What topping would you like on your pizza?
Enter 'quit' when you are finished: quit
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 tel them the cost of their movie ticket.
prompt = "How old are you?"
prompt += "\nEnter 'quit' when you are finished. "
while True:
age = input(prompt)
if age == 'quit':
break
age = int(age)
if age < 3:
print(throwable有两个直接子类是" You get in free!")
elif age < 13:
print(python编程基础教程课后答案" Your ticket is $10.")
else:
print(" Your ticket is $15.")
Output:
How old are you?
Enter 'quit' when you are finished. 2
You get in free!
How old are you?
Enter 'quit' when you are finished. 3
Your ticket is $10.
How old are you?
Enter 'quit' when you are finished. 12
Your ticket is $10.
How old are you?
Enter 'quit' when you are finished. 18
Your ticket is $15.
How old are you?
Enter 'quit' when you are finished. quit
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.
sandwich_orders = ['veggie', 'grilled cheese', 'turkey', 'roast beef']
finished_sandwiches = []
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论