-
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
(
)
print
(
+
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
=
input
(
tonight?
)
party_size
=
int
(party_size)
if
party_size
>
8
:
print
(
)
else
:
print
(
)
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
=
< br>input
(
)
number
=
int
(number)
if
number
%
10
==
0
:
print
(
str
(number)
+
)
else
:
print
(
str
(num
ber)
+
)
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 th
eir pizza.
prompt
=
prompt
+=
while
True
:
topping
=
input
(prompt)
if
topping
!=
'quit'
:
print
(
+
topping
+
)
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
=
prompt
+=
while
True
:
age
=
input
(prompt)
if
age
==
'quit'
:
break
age
=
int
(age)
if
age
<
3
:
print
(
)
elif
age
<
13
:
print
(
)
else
:
print
(
)
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,
-
-
-
-
-
-
-
-
-
上一篇:playway2-内容
下一篇:如何量测LED