python - 如何从python中的条件列表中选择一个随机单词
问题描述
我是一名初级编码员,我想从我的列表中选择一个带有几个条件的随机元素,并且只有如果条件匹配,应该打印随机词,你能否建议一个可以在 python 3 中工作的代码。这里是代码
`import time
import random
wordbank = [" the " ,
" of " ,
" and " ,
" to " ,
" a " ,
" in " ,
" that " ,
" I " ,
" was " ,
" he " ,
" his " ,
" with " ,
" is " ,
" it " ,
" for " ,
" as " ,
" had " ,
" you " ,
" not " ,
" be " ,
" on " ,
" at " ,
" by " ,
" her " ,
" which " ,
" have " ,
" or " ,
" from " ,
" this " ,
" but " ]
print("hey this is the champion AI robot which no human can beat in
playing word bank")
quasimodo = input("want to know how it is played y/n")
if quasimodo == "y" :
print("so each player is going to start by saying a word and the
other person is supposed to tell a word by the ending letter of the word
and the loop continues untill one person cant tell a word in 20 secs")
print("lets play i mean how long can a human last in front of a computer")
print ("because of my kindness i will let you start prepare to lose")
while 7 < 8 :
a = input()
b = len(a)
c = b + 1
d = random.choice(wordbank)
e = a[c]
f = len(d)
g = f + 1
h = d[g]
解决方案
import random
lst = ['a','b','c']
print(random.choice(lst))
(使用随机模块来获取随机值,而不是经历创建随机发生器的漫长过程)
推荐阅读
- elasticsearch - ElasticSearch 按文档字段分组并计算出现次数
- python - 正则表达式在下划线和句点之后查找日期格式
- java - java双面韦尔奇t检验
- amazon-web-services - 无法解压缩上传的文件。请检查您的文件,然后尝试重新上传错误
- macos - 如何在启动时在 Mac Pro 服务器上挂载远程文件夹而无需用户登录?
- java - 如何在 xml 文件中查找 id?
- android - 如何使 View 的高度与 NestedScrollView 上方的空间相匹配?
- c# - 在上下文中设置实体框架连接字符串
- android - 如何保护我的应用免受 Google 本身的攻击?
- ios - 如何将 coredata 模型转换为 swift struct 模型?