# 7.3.1 在列表之間移動元素app
# confirmed_users.py函數
# 首先,建立一個待驗證用戶列表spa
# 和一個用於存儲已驗證用戶的空列表rem
uncomfirmed_users = ['james','star','wendy']get
comfirmed_users = []input
# 驗證每一個用戶,直到沒有未驗證用戶爲止it
# 將每一個通過驗證的列表都移到已驗證用戶列表中table
while uncomfirmed_users:
current_user = uncomfirmed_users.pop() # pop.() 默認移除列表中最後一個元素
print("Verifying use: " + current_user.title())
comfirmed_users.append(current_user) # 空列表已經在這段碼塊中更新
# 顯示全部已驗證的用戶
print("\nThe following users have been comfirmed:")
for comfirmed_user in comfirmed_users:
print(comfirmed_user.title())ast
# 7.3.2 刪除包含特定值的全部列表元素
#pets.py
pets = ['dog','cat','goldfish','cat','rabbit','tigger','cat','chick']cli
# 循環尋找列表中的'cat',而且移除全部'cat',直到列表中沒有這條信息
while 'cat' in pets:
pets.remove('cat')
print(pets)
# 7.3.3 使用用戶輸入來填充字典
# mountain_poll.py
responses = {}
polling_active = True
while polling_active:
# input()函數,提示用戶須要輸入信息
name = input("What's your name?")
response = input("Which mountain would you like to climb someday?")
# 字典 {'keys','values'} 等效於 keys = values,name = response
responses[name] = response
repeat = input("would you like to let another person repond? (yes/no)")
if repeat == 'no':
polling_active = False
print("\n---Poll Result---")
for name, response in responses.items():
print(name.title() + " would like to climb " + response + ".")
# 7-8 熟食店
sandwich_orders = ['tomato','potato','vegetable']
finished_sandwiches = []
while sandwich_orders:
current_sandwich = sandwich_orders.pop()
print(current_sandwich + ", I made your tuna sandwich.")
finished_sandwiches.append(current_sandwich)
print("All the sandwiches are finished.")
for finished_sandwich in finished_sandwiches:
print(finished_sandwich.title())
# 7-9 五香菸薰牛肉
sandwich_orders = ['tomato','pastrami','potato','pastrami','vegetable','pastrami']
finished_sandwiches = []
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
print("Pastrami sold out!")
while sandwich_orders:
current_sandwich = sandwich_orders.pop()
print(current_sandwich + ", I made your tuna sandwich.")
finished_sandwiches.append(current_sandwich)
print("All the sandwiches are finished.")
for finished_sandwich in finished_sandwiches:
print(finished_sandwich.title())
# 7-10 夢想的度假勝地
responses = {}
polling_active = Truewhile polling_active: name = input("What's 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 respond? (yes/no) ") if repeat == 'no': polling_active = False print("---Poll Result---")for name, response in responses.items(): print(name.title() + " would like to visit " + response + ".")