第七章 用户输入和while循环
动手试一试代码实现
(1)习题内容
7-1 汽车租赁:编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如“Let me see if I can find you a Subaru”。
7-2 餐馆订位:编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息,指出没有空桌;否则指出有空桌。
7-3 10的整数倍:让用户输入一个数字,并指出这个数字是否是10的整数倍。
7-4 比萨配料:编写一个循环,提示用户输入一系列的比萨配料,并在用户输入‘quit’时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。
7-5 电影票:有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。
备注:7-5练习题的代码实现在7-6练习题中。
7-6 三个出口:以另一种方式完成练习7-4或练习7-5,在程序中采取如下所有做法。
①在while循环中使用条件测试来结束循环。(备注:不单独编写)
②使用变量active来控制循环结束的时机。
③使用break语句在用户输入‘quit’时退出循环。
7-8 熟食店:创建一个名为 sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为finished_sandwiches的空列表。遍历列表sandwich_orders,对于其中的每种三明治,都打印一条消息,如 I made your tuna sandwich,并将其移到列表finish_sandwiches。所有三明治都制作好后,打印一条消息,将这些三明治列出来。
7-9 五香烟熏牛肉(pastrami)卖完了:使用为完成练习 7-8 而创建的列表sandwich_orders,并确保‘pastrami’在其中至少出现了三次。在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while循环将列表sandwich_orders中的‘pastrami’都删除。确认最终的列表finish_sandwiches中不包含‘pastrami’。
7-10 梦想的度假胜地:编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one place in the world,where would you go?”的提示,并编写一个打印调查结果的代码块。

#  7-1 
car=input("Please tell me what's car you want: \n")
print("Let me see if I can find you a "+car+".")
#  7-2
meal_number=input("\nExcuse me,please tell me how many people have meal:")
meal_number=int(meal_number)
if meal_number>8:
	print("Excuse me,there is not empty table.")
else:
	print("There is empty table.")
#  7-3
number=input("\nPlease input a number,I will tell you if it can be divisible by 10:")
number=int(number)
if number % 10 == 0:
	print("it can be divisible by 10.")
else:
	print("it can't be divisible by 10.")
#  7-4
pizza_ingredient="\nPlease input a ingredient of pizza:"
pizza_ingredient+="\nEnter 'quit' when you are finished."
ingredients=""
while ingredients != 'quit':
	ingredients=input(pizza_ingredient)
	if ingredients != 'quit':
		print("We will add "+ingredients+" in pizza.")
#  7-6_2
user_age2="\nPlease tell me your age:"
user_age2+="\nEnter 'quit' end the program."
active=True
while active:
	age=input(user_age2)
	if age == 'quit':
		active=False
	elif int(age) < 3:
		print("The ticket price is free.")
	elif int(age) <= 12:
		print("The ticket price is 10.")
	elif int(age) > 12:
		print("The ticket price is 15.")
#  7-6_3
user_age3="\nPlease tell me your age:"
user_age3+="\nEnter 'quit' end the program."
while True:
	age=input(user_age3)
	if age == 'quit':
		break
	elif int(age) < 3:
		print("The ticket price is free.")
	elif int(age) <= 12:
		print("The ticket price is 10.")
	elif int(age) > 12:
		print("The ticket price is 15.")
print("\n")
#  7-8
sandwich_orders=['egg','beef','tuna']
finished_sandwiches=[]
while sandwich_orders:
	current_sandwich=sandwich_orders.pop()
	print("I made your "+current_sandwich+" sandwich.")
	finished_sandwiches.append(current_sandwich)
print("\nThe following sandwiches have been made:")
for finished_sandwich in finished_sandwiches:
	print(finished_sandwich.title()+" sandwich")
#  7-9
sandwich_orders=['egg','pastrami','beef','pastrami','tuna','pastrami']
print("\nThe deli's pastrami is sold out.")
while 'pastrami' in sandwich_orders:
	sandwich_orders.remove('pastrami')
finished_sandwiches=sandwich_orders
print("The sandwich orders:")
print(finished_sandwiches)
#  7-10
survey={}
survey_actice=True
while survey_actice:
	respondent=input("\nWhat is your name?")
	reply=input("Hello,"+respondent.title()+",if you could visit one place in the world,where would you go?")
	survey[respondent]=reply
	go_on=input("Is there anyone else who wants to participate in the investigation?(yes/no)")
	if go_on == 'no':
		survey_actice=False
print("\n--- survey results ---")
for respondent,reply in survey.items():
	print(respondent.title()+" would like to visit "+reply.title()+".")