7-1 汽车租赁 :编写一个程序,询问用户要租赁什么样的汽车,并打印一条消息,如 “Let me see if I can find you a Subaru” 。
message = input("what kind of cars do you wan to rent ? ")
print("Let me see if i can find you a Subaru")
7-2 餐馆订位 :编写一个程序,询问用户有多少人用餐。如果超过 8 人,就打印一条消息,指出没有空桌;否则指出有空桌。
people_counting= input("how many customers eat ?")
people_counting = int(people_counting)
if people_counting > 8:
print("Sorry,We don't have an empty table now ")
else:
print("Please come in ,we have empty table now")
7-3 10 的整数倍 :让用户输入一个数字,并指出这个数字是否是 10 的整数倍。
number = input("Please enter a number")
number = int(number)
if number % 10 == 0:
print(str(number) + " is multiples of 10")
else:
print(str(number) + " not multiples of 10")
7-8 熟食店 :创建一个名为 sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为 finished_sandwiches 的空列表。遍历列
表 sandwich_orders ,对于其中的每种三明治,都打印一条消息,如 I made your tuna sandwich ,并将其移到列表 finished_sandwiches 。所有三明治都制作好后,打印一条消息,将这些三明治列出来。
sandwich_orders = ['pineapple_chiken_sandwich', 'lemon_sandwich', 'peach_beef_sandwich','sugar_cane_sandwich', 'apricot_sandwich', 'plum_sandwich']
finished_sandwiches = []
while sandwich_orders:
current_order = sandwich_orders.pop()
print("I made your : " + current_order.title())
finished_sandwiches.append(current_order)
print("\nThe following order have been finished:")
for finished_sandwiche in finished_sandwiches:
print(finished_sandwiche.title())
7-9 五香烟熏牛肉( pastrami )卖完了 :使用为完成练习 7-8 而创建的列表 sandwich_orders ,并确保 ‘pastrami’ 在其中至少出现了三次。在程序开头附近添加这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个 while 循环将列表 sandwich_orders 中的 ‘pastrami’ 都删除。确认最终的列表 finished_sandwiches 中不包含 ‘pastrami’ 。
sandwich_orders = ['pastrami_sandwich', 'pineapple_chiken_sandwich', 'lemon_sandwick', 'peach_beef_sandwick',
'sugar_cane_sandwich', 'pastrami_sandwich', 'apricot_sandwich', 'pastrami_sandwich', 'plum_sandwich']
while 'pastrami_sandwich' in sandwich_orders:
sandwich_orders.remove('pastrami_sandwich')
finished_sandwiches = []
while sandwich_orders:
current_order = sandwich_orders.pop()
print("I made your : " + current_order.title())
finished_sandwiches.append(current_order)
while 'pastrami_sandwich' in sandwich_orders:
sandwich_orders.remove('pastrami_sandwich')
print("\nThe following order have been finished:")
for finished_sandwiche in finished_sandwiches:
print(finished_sandwiche.title())
7-10 梦想的度假胜地 :编写一个程序,调查用户梦想的度假胜地。使用类似于 “If you could visit one place in the world, where would you go?” 的提示,并编写一个打印调查结果的代码块。
responses = {}
polling_active = True
while polling_active:
name = input("\nWhat is your name? ")
response = input("If you could visit one place in the world, where would you go ?")
responses[name] = response
repeat = input("Would you like to let another person respod? (yes/ no) ")
if repeat == 'no':
polling_active = False
print("\n--- Poll Results ---")
for name,response in responses.items():
print(name + " would like to go to " + response + ".")