7.3 使用while 循环来处理列表和字典

# 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 + ".")

相关文章
相关标签/搜索