来自set的random.choice? Python [英] random.choice from set? python

查看:98
本文介绍了来自set的random.choice? Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事猜谜游戏的AI部分.我希望AI从此列表中选择一个随机字母.我将其设置为一组,这样我就可以轻松地从列表中删除字母,因为它们在游戏中会被猜到,因此不再可以再次被猜到.

I'm working on an AI portion of a guessing game. I want the AI to select a random letter from this list. I'm doing it as a set so I can easily remove letters from the list as they are guessed in the game and are therefore no longer available to be guessed again.

表示set对象不可索引.我该如何解决?

it says set object isn't indexable. How can I work around this?

import random 
aiTurn=True

while aiTurn == True:
    allLetters = set(list('abcdefghijklmnopqrstuvwxyz'))
    aiGuess=random.choice(allLetters)



    print (aiGuess) 

推荐答案

>>> random.sample(set('abcdefghijklmnopqrstuvwxyz'), 1)
['f']

文档: https://docs.python.org/3/library/random.html#random.sample

这篇关于来自set的random.choice? Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆