使用列表失败消息

问题描述 投票:0回答:1

好吧,基本上。我正在编写一个随机脚本来“查找衣服”,衣服在列表中。所以我们把鞋子之类的东西都列在了一个清单里。尽管我似乎无法弄清楚如何仅打印列表中的一个元素。 我原来用的是

random.choice(clothes)
但后来我不知道如何从列表中删除该选项,因为如果这是一个选择,我会这样做。

python replit
1个回答
0
投票

如果我理解正确的话,你的意思是这样的,你可以使用删除方法。 https://docs.python.org/3/tutorial/datastructs.html

按照你的逻辑,你可以这样解决:

import random

clothes = ['shirt', 'pants', 'shoes', 'hat', 'jacket']

chosen_item = random.choice(clothes)

print("Chosen item:", chosen_item)

clothes.remove(chosen_item)

print(clothes)
© www.soinside.com 2019 - 2024. All rights reserved.