1 prompt = "\nPlease tell me your age."
2 prompt += "\nEnter 'quit' to end."
3 message = ''
4 switch = True
5 while switch:
6
7 message = input(prompt)
8 if message == 'quit':
9 switch = False
10 else:
11 message = int(message)
12 if message < 3:
13 price = 0
14 if message >= 3 and message <= 12:
15 price = 10
16 if message > 12:
17 price = 15
18 print("Your ticket cost is $" + str(price) + ".")
19
20
21 unconfiemed_users = ['aa','bb','cc']
22 confiemrd_users = []
23 while unconfiemed_users:
24 current_user = unconfiemed_users.pop()
25
26 print("Verifying user:" + current_user.title())
27 confiemrd_users.append(current_user)
28
29 print("\nThe following users have been confirmed:")
30 for confirmed_user in confiemrd_users:
31 print(confirmed_user.title())
32
33 pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
34 print(pets)
35 while 'cat' in pets:
36 pets.remove('cat')
37 print(pets)
38 """
39 responses = {} # 设置一个标志,指出调查是否继续 经典
40
41 polling_active = True
42 while polling_active: # 提示输入被调查者的名字和回答
43
44 name = input("\nWhat is your name? ")
45 response = input("Which mountain would you like to climb someday? ")
46
47 responses[name] = response # 将答卷存储在字典中
48
49 repeat = input("Would you like to let another person respond? (yes/ no) ")# 看看是否还有人要参与调查
50 if repeat == 'no':
51 polling_active = False
52
53 print("\n--- Poll Results ---")# 调查结束,显示结果
54 for name, response in responses.items():
55 print(name + " would like to climb " + response + ".")
56 """
57
58
59
60 # 练习7-8
61
62 sandwich_orders = ['aa', 'bb', 'cc']
63 finished_sandwiches = []
64 while sandwich_orders:
65 making_sandwich = sandwich_orders.pop()
66 print("\nI made your " + making_sandwich + " sandwiches.")
67 # finished_sandwiches.append(finished_sandwiches) # >>>[[...], [...], [...]]问题
68 finished_sandwiches.append(making_sandwich)
69 print(finished_sandwiches)
70
71 sandwich_orders = ['aa', 'bb', 'cc', 'pastrami','pastrami','pastrami']
72 print()
73 while 'pastrami' in sandwich_orders:
74 sandwich_orders.remove('pastrami')
75 print(sandwich_orders)
76 if 'pastrami' not in sandwich_orders:
77 print('Pastrami was sold out.')
78
79 # 定义为形,调用为实。
80 # 练习8-1
81 def display_message(learn):
82 """What did I learned?"""
83 print("I learned " + learn + " in this.")
84
85
86 display_message("kannsu")
87
88
89 def favorite_book(title):
90 """print my favorite book"""
91 print("One of my favorite books is " + title + ".")
92
93 favorite_book("Building.Machine.Learning.Systems.with.Python")