7-1 汽车租赁 :编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如“LetmeseeifIcan find you a Subaru”。
car = input("What kind of car do you want to lease? ")
print("Let me see if I can find you a " + car + ".")
What kind of car do you want to lease? Subaru
Let me see if I can find you a Subaru.
7-2 餐馆订位 :编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息,指出没有空桌;否则指出有空桌。
people = input("How many people for dinner? ")
people = int(people)
if people > 8:
print("There is no table available.")
else:
print("There are some tables free.")
How many people for dinner? 5
There are some tables free.
How many people for dinner? 9
There is no table available.
7-3 10的整数倍 :让用户输入一个数字,并指出这个数字是否是10的整数倍。
number = input("Please input a number: ")
if int(number) % 10 == 0:
print(number + " is a mutiple of 10.")
else:
print(number + " is not a mutiple of 10.")
Please input a number: 30
30 is a mutiple of 10.
Please input a number: 22
22 is not a mutiple of 10.
7-4 比萨配料 :编写一个循环,提示用户输入一系列的比萨配料,并在用户输入'quit' 时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。
while True:
topping = input("What pizza topping would you like to add?: ")
if(topping == "quit"):
break;
else:
print("We will add " + topping + ".")
What pizza topping would you like to add?: mushrooms
We will add mushrooms.
What pizza topping would you like to add?: french fries
We will add french fries.
What pizza topping would you like to add?: quit
7-5 电影票 :有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。
while True:
old = int(input("How old are you? "))
if old < 3:
print("The fare is free.")
elif old <= 12:
print("The fare is 10 dollars.")
else:
print("The fare is 12 dollars.")
How old are you? 2
The fare is free.
How old are you? 7
The fare is 10 dollars.
How old are you? 14
The fare is 12 dollars.
How old are you?
7-8 熟食店 :创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为finished_sandwiches 的空列表。遍历列表sandwich_orders ,对于其中的每种三明治,都打印一条消息,如I made your tuna sandwich ,并将其移到列表finished_sandwiches 。所有三明治都制作好后,打印一条消息,将这些三明治列出来。
sandwich_orders = ["tuna","chicken","vegetable salad"]
finished_sandwiches = []
while sandwich_orders:
sandwich = sandwich_orders.pop()
print("I made your " + sandwich + " sandwich.")
finished_sandwiches.append(sandwich)
print("All finished sandwiched: ",end="")
print(finished_sandwiches)
I made your vegetable salad sandwich.
I made your chicken sandwich.
I made your tuna sandwich.
All finished sandwiched: ['vegetable salad', 'chicken', 'tuna']
7-9 五香烟熏牛肉(pastrami)卖完了 :使用为完成练习7-8而创建的列表sandwich_orders ,并确保'pastrami' 在其中至少出现了三次。在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while 循环将列表sandwich_orders 中的'pastrami' 都删除。确认最终的列表finished_sandwiches 中不包含'pastrami'。
sandwich_orders = ["tuna","pastrami","chicken","pastrami","vegetable salad","pastrami"]
print("Pastrami is sold out.")
while "pastrami" in sandwich_orders:
sandwich_orders.remove("pastrami");
print("All sandwich_orders are ",end="");
print(sandwich_orders)
Pastrami is sold out.
All sandwich_orders are ['tuna', 'chicken', 'vegetable salad']
7-10 梦想的度假胜地 :编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one placein the world, where would you go?”的提示,并编写一个打印调查结果的代码块。
responses = {}
survey_active = True
name_message = "What's your name? "
response_message = "If you could visit one placein the world,"
response_message += " where would you go? "
repeat_message = "Would you like to let another person response?"
repeat_message += " (yes/no) "
while survey_active:
name = input(name_message)
response = input(response_message)
responses[name] = response
repeat = input(repeat_message)
while repeat.upper() != "YES" and repeat.upper() != "NO":
repeat = input(repeat_message)
if repeat == "no":
survey_active = False;
print()
print("\n--- Survey Results ---")
for name,response in responses.items():
print(name.title() + " would like to visit ",end="")
print(response.title() + ".")
What's your name? alice
If you could visit one placein the world, where would you go? china
Would you like to let another person response? (yes/no) yes
What's your name? mike
If you could visit one placein the world, where would you go? japan
Would you like to let another person response? (yes/no) no
--- Survey Results ---
Alice would like to visit China.
Mike would like to visit Japan.