我有一个清单叫做 gamegame = ['X','O',' ',' ',' ',' ',' ',' ',' ']我只需要从所有' '元素中选择一个随机元素。然后将其更改为X或O,然后再次执行。使用简单random.choice()有可能永无止境。random.choices()返回元素而不是其索引,因此我无法想到一种更新权重的方法。
1 回答

翻翻过去那场雪
TA贡献2065条经验 获得超14个赞
确定哪些索引具有空格并用于random.choice选择一个
from random import choice
game = ['X','O',' ',' ',' ',' ',' ',' ',' ']
index = choice([i for i, x in enumerate(game) if x == ' '])
添加回答
举报
0/150
提交
取消