7-1 汽车租赁:编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,
如“Let me see if I can find you a Subaru”。
car = input('What kind of car do you want to rent?')
print('Let me see if I can find you a ' + car)
运行结果:
What kind of car do you want to rent?Subaru
Let me see if I can find you a Subaru
7-2 餐馆订位:编写一个程序,询问用户有多少人用餐。如果超过 8 人,就打印一
条消息,指出没有空桌;否则指出有空桌。
num = input('how many people?')
num = int(num)
if num > 8:
print('Sorry,No seats.')
else:
print('Your table is ready.')
运行结果:
how many people?10
Sorry,No seats.
7-3 10的整数倍:让用户输入一个数字,并指出这个数字是否是 10 的整数倍。
number = input('Integer multiples of 10? :')
number = int(number)
if number % 10 == 0:
print('yes')
else:
print('no')
运行结果:
Integer multiples of 10? :15
no
7-4 比萨配料:编写一个循环,提示用户输入一系列的比萨配料,并在用户输入’quit’时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。
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
运行结果:
What topping would you like on your pizza?
Enter 'quit' when you are finished: fruit
I'll add fruit to your pizza.
What topping would you like on your pizza?
Enter 'quit' when you are finished: seafood
I'll add seafood to your pizza.
What topping would you like on your pizza?
Enter 'quit' when you are finished: quit
7-5 电影票:有家电影院根据观众的年龄收取不同的票价:不到 3 岁的观众免费;3~12 岁的观众为 10 美元;超过 12 岁的观众为 15 美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。
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(" You get in free!")
elif age < 13:
print(" Your ticket is $10.")
else:
print(" Your ticket is $15.")
运行结果:
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. 11
Your ticket is $10.
How old are you?
Enter 'quit' when you are finished. 30
Your ticket is $15.
How old are you?
Enter 'quit' when you are finished.
7-6 三个出口:以另一种方式完成练习 7-4 或练习 7-5,在程序中采取如下所有做法。
#在 while 循环中使用条件测试来结束循环。
prompt = '\nTell me something, and I will repeat it back to you:'
prompt += "\nEnter 'quit' to end the game."
messsge = ''
while messsge != 'quit':
messsge = input(prompt)
if messsge != 'quit':
print(messsge)
#使用变量 active 来控制循环结束的时机。
prompt = '\nTell me something, and I will repeat it back to you:'
prompt += "\nEnter 'quit' to end the game."
active = True
while active:
messsge = input(prompt)
if messsge == 'quit':
active = False
else:
print(messsge)
# 使用 break 语句在用户输入'quit'时退出循环。
prompt = '\nTell me something, and I will repeat it back to you:'
prompt += "\nEnter 'quit' to end the game."
while True:
city = input(prompt)
if city == 'quit':
break
else:
print(messsge)
运行结果:
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the game.hello
hello
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the game.quit
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the game.world
world
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the game.quit
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the game.python
quit
Tell me something, and I will repeat it back to you:
Enter 'quit' to end the game.quit
7-7 无限循环:编写一个没完没了的循环,并运行它(要结束该循环,可按 Ctrl +C,也可关闭显示输出的窗口)。
while True:
print("Hello World!")
7-8 熟食店:创建一个名为 sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为 finished_sandwiches 的空列表。遍历列表sandwich_orders,对于其中的每种三明治,都打印一条消息,如 I made your tuna sandwich,并将其移到列表finished_sandwiches。所有三明治都制作好后,打印一条消息,将这些三明治列出来。
sandwich_orders = ['small_sandwich','middle_sandwich','big_sandwish']
finished_sandwiches=[]
while sandwich_orders:
current_order = sandwich_orders.pop()
print("I made your " + current_order)
finished_sandwiches.append(current_order)
for finished_sandwich in finished_sandwiches:
print(finished_sandwich +" has been made")
运行结果:
I made your big_sandwish.
I made your middle_sandwich.
I made your small_sandwich.
big_sandwish has been made.
middle_sandwich has been made.
small_sandwich has been made.
7-9 五香烟熏牛肉(pastrami)卖完了:使用为完成练习 7-8 而创建的列表sandwich_orders,并确保’pastrami’在其中至少出现了三次。在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个 while 循环将列表 sandwich_orders 的’pastrami’都删除。确认最终的列表 finished_sandwiches 中不包含’pastrami’。
sandwich_orders = ['pastrami','pastrami','big_sandwish','pastrami']
print("The pastrami was sold out")
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
print(sandwich_orders)
运行结果:
The pastrami was sold out
['big_sandwish']
7-10 梦想的度假胜地:编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one place in the world, where would you go?”的提示,并编写一个打印调查结果的代码块。
polling_active = True
while polling_active:
#提示输入调查者的名字和回答
name = input("\nWhat's your name?")
response = input("if you could visit one place in the world,where would u go?")
#将答案存储在字典中
responses[name] = response
#看看是否还有人要参与调查
repeat = input("Would you like to let another person respond? (yes/no)")
if repeat == 'no':
polling_active = False
运行结果:
What's your name?john
if you could visit one place in the world,where would u go?Shanghai
Would you like to let another person respond? (yes/no)no