章节代码及练习
#@使用while 循环来处理列表和字典
#@在列表之间移动元素
------------------------------------------------------------------
#首先,创建一个待验证用户列表
#和一个用于存储已验证用户的空列表
unconfirmed_users = ['alice', 'brian', 'candace']
confirmed_users = []
# 验证每个用户,直到没有未验证用户为止
#将每个经过验证的列表都移到已验证用户列表中
while unconfirmed_users:
#pop() 以每次一个的方式从列表unconfirmed_users 末尾删除未验证的用户
current_user = unconfirmed_users.pop()
print(current_user)
print("Verifying user: " + current_user.title())
#将删除的元素添加到confirmed_users列表中
confirmed_users.append(current_user)
print(confirmed_users)
# 显示所有已验证的用户
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
#删除包含特定值的所有列表元素
pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
#while 'cat' in pets:
while 'cat' in pets:
pets.remove('cat')
print(pets)
#@使用户输入来填充字典
responses = {}
# 设置一个标志,指出调查是否继续
polling_active = True
while polling_active:
# 提示输入被调查者的名字和回答
name = input("\nWhat is your name? ")
response = input("Which mountain would you like to climb someday? ")
# 将答卷存储在字典中
responses[name] = response
print(responses)
# 看看是否还有人要参与调查
repeat = input("Would you like to let another person respond? (yes/ no) ")
if repeat == 'no':
polling_active = False
# 调查结束,显示结果
print("\n--- Poll Results ---")
for name, response in responses.items():
print(name + " would like to climb " + response + ".")
#-------------------------------------------------------------------------------------------------
'''
@@@
7-8 熟食店 :创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为finished_sandwiches 的空列表。遍历列
表sandwich_orders ,对于其中的每种三明治,都打印一条消息,如I made your tuna sandwich ,并将其移到列表finished_sandwiches 。所有三明
治都制作好后,打印一条消息,将这些三明治列出来。
7-9 五香烟熏牛肉((pastrami))卖完了了 :使用为完成练习7-8而创建的列表sandwich_orders ,并确保'pastrami' 在其中至少出现了三次。在程序开头附近添加
这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while 循环将列表sandwich_orders 中的'pastrami' 都删除。确认最终的列
表finished_sandwiches 中不包含'pastrami' 。
7-10 梦想的度假胜地 :编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one place in the world, where would you go?”的提示,并编写一个打印调查
结果的代码块。
@@@
'''
#-----------------------------------------------------------------------------------------------
sandwich_orders = ['beef','tomoto','apple']
finished_sandwiches = []
while sandwich_orders:
remove_foods = sandwich_orders.pop()
print(remove_foods)
print("\nI made your tuna " + remove_foods)
finished_sandwiches.append(remove_foods)
print(finished_sandwiches)
for sandwichs in finished_sandwiches:
print("\nYou had finished " + sandwichs)
#----------------------------------------------------------------------------------------------
sandwich_orders = ['pastrami','beef','pastrami','hotdog','pastrami','apple']
finished_sandwiches = ['sss']
print('熟食店的五香烟熏牛肉卖完了')
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
finished_sandwiches += sandwich_orders
print(finished_sandwiches)
#----------------------------------------------------------------------------------------------
visit_plance = {}
ask_active = True
while ask_active:
name = input("\nWhat's your name?")
plance = input("If you could visit one place in the world, where would you go?")
#将键和值添加到字典
visit_plance[name] = plance
print(visit_plance)
print(visit_plance[name])
other = input("Any other places want to go?(Yes/No)")
if other == 'no':
ask_active = False
print("\n---------Display results---------")
for name, plance in visit_plance.items():
print(name + "would like go to" + plance + ".")
#print(name,plance)