pythonwhile列表_7.3 使用while 循环来处理列表和字典|python基础教程|python入门|python教程...

https://www.xin3721.com/eschool/pythonxin3721/

#7.3.1 在列表之间移动元素

# confirmed_users.py

# 首先,创建一个待验证用户列表

# 和一个用于存储已验证用户的空列表

uncomfirmed_users = ['james','star','wendy']

comfirmed_users = []

# 验证每个用户,直到没有未验证用户为止

# 将每个经过验证的列表都移到已验证用户列表中

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())

# 7.3.2 删除包含特定值的所有列表元素

#pets.py

pets = ['dog','cat','goldfish','cat','rabbit','tigger','cat','chick']

# 循环寻找列表中的'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 = True

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值