第七章
7.1
练习7-1:汽车租赁 编写一个程序,询问用户要租赁什么样的汽车,并打印 一条消息,下面是一个例子。 Let me see if I can find you a Subaru.
car = input("What kind of car would you like to rent? ")
print(f"Let me see if I can find you a {car}.")
练习7-2:餐馆订位 编写一个程序,询问用户有多少人用餐。如果超过8位, 就打印一条消息,指出没有空桌;否则指出有空桌。
num_people = int(input("How many people are in your party? "))
if num_people > 8:
print("Sorry, there are no available tables.")
else:
print("There are available tables for your party.")
练习7-3:10的整数倍 让用户输入一个数,并指出该数是否是10的整数倍
number = int(input("Enter a number: "))
if number % 10 == 0:
print(f"{number} is a multiple of 10.")
else:
print(f"{number} is not a multiple of 10.")
7.2
练习7-4:比萨配料
编写一个循环,提示用户输入一系列比萨配料,并在用户输入'quit'时结束循环。每当用户输入一种配料后,都打印一条消息,指出我们会在比萨中添加这种配料。
print("Enter the pizza toppings you want to add.")
print("Enter 'quit' when you are finished.")
while True:
topping = input("Enter a pizza topping: ")
if topping == 'quit':
break
else:
print(f"We will add {topping} to the pizza.")
练习7-5:电影票
有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众收费10美元;超过12岁的观众收费15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。
while True:
age = input("Enter your age (enter 'quit' to exit): ")
if age == 'quit':
break
age = int(age)
if age < 3:
print("You get in for free!")
elif age <= 12:
print("The ticket price is $10.")
else:
print("The ticket price is $15.")
练习7-6:三种出路
以不同的方式完成练习7-4或练习7-5,在程序中采取如下做法。
在while循环中使用条件测试来结束循环。
print("Enter the pizza toppings you want to add.")
print("Enter 'quit' when you are finished.")
topping = ''
while topping != 'quit':
topping = input("Enter a pizza topping: ")
if topping != 'quit':
print(f"We will add {topping} to the pizza.")
使用变量active来控制循环结束的时机。
print("Enter the pizza toppings you want to add.")
print("Enter 'quit' when you are finished.")
active = True
while active:
topping = input("Enter a pizza topping: ")
if topping == 'quit':
active = False
else:
print(f"We will add {topping} to the pizza.")
使用break语句在用户输入'quit'时退出循环。
print("Enter the pizza toppings you want to add.")
print("Enter 'quit' when you are finished.")
while True:
topping = input("Enter a pizza topping: ")
if topping == 'quit':
break
else:
print(f"We will add {topping} to the pizza.")
练习7-7:无限循环
编写一个没完没了的循环,并运行它(要结束该循环,可按Ctrl + C,也可关闭显示输出的窗口)。
while True:
print("This is an infinite loop!")
7.3
练习7-8:熟食店
创建一个名为sandwich_orders的列表,在其中包含各种三明治的名字,再创建一个名为finished_sandwiches的空列表。遍历列表sandwich_orders,对于其中的每种三明治,都打印一条消息,如"I made your tuna sandwich",并将其移到列表finished_sandwiches中。所有三明治都制作好后,打印一条消息,将这些三明治列出来。
sandwich_orders = ['tuna', 'ham and cheese', 'turkey', 'pastrami', 'pastrami', 'pastrami']
finished_sandwiches = []
while sandwich_orders:
current_sandwich = sandwich_orders.pop(0)
print(f"I made your {current_sandwich} sandwich.")
finished_sandwiches.append(current_sandwich)
print("\nList of finished sandwiches:")
for sandwich in finished_sandwiches:
print(sandwich)
练习7-9:五香烟熏牛肉卖完了
使用为完成练习7-8而创建的列表sandwich_orders,并确保'pastrami'在其中至少出现了三次。在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉(pastrami)卖完了;再使用一个while循环将列表sandwich_orders中的'pastrami'都删除。确认最终的列表finished_sandwiches未包含'pastrami'。
sandwich_orders = ['tuna', 'ham and cheese', 'pastrami', 'turkey', 'pastrami', 'pastrami']
finished_sandwiches = []
print("Sorry, we're out of pastrami.")
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
while sandwich_orders:
current_sandwich = sandwich_orders.pop(0)
print(f"I made your {current_sandwich} sandwich.")
finished_sandwiches.append(current_sandwich)
print("\nList of finished sandwiches:")
for sandwich in finished_sandwiches:
print(sandwich)
练习7-10:梦想的度假胜地
编写一个程序,调查用户梦想的度假胜地。使用类似于下面的提示,并编写一个打印调查结果的代码块。
dream_vacation = input("If you could visit one place in the world, where would you go? ")
print(f"Your dream vacation spot is {dream_vacation}.")