|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
附上代码
sandwich_orders = ["lisa", "ben", "kevin","alice"]
fished_sandwiches = []
for people in sandwich_orders:
print("hello " + people)
print(sandwich_orders)
sandwich_orders.remove(people)
print(sandwich_orders)
sandwich = input("which kind sandwich would you like ?")
print("i mande your " + sandwich + " sanwich")
fished_sandwiches.append(sandwich)
print("you sandwich is finished")
print(fished_sandwiches)
print(sandwich_orders)
输出结果:
hello lisa
['lisa', 'ben', 'kevin', 'alice']
['ben', 'kevin', 'alice']
which kind sandwich would you like ?a
i mande your a sanwich
you sandwich is finished
hello kevin
['ben', 'kevin', 'alice']
['ben', 'alice']
which kind sandwich would you like ?b
i mande your b sanwich
you sandwich is finished
['a', 'b']
['ben', 'alice']
Process finished with exit code 0
remove() 在遍历中使用通常都是陷阱。。
代码帮你改好了:
- sandwich_orders = ["lisa", "ben", "kevin", "alice"]
- fished_sandwiches = []
- for people in sandwich_orders[:]:
- print("hello " + people)
- print(sandwich_orders)
- sandwich_orders.remove(people)
- print(sandwich_orders)
- sandwich = input("which kind sandwich would you like ?")
- print("i mande your " + sandwich + " sanwich")
- fished_sandwiches.append(sandwich)
- print("you sandwich is finished")
- print(fished_sandwiches)
- print(sandwich_orders)
复制代码
|
|