我有一张单子叫“打孔”:
punch = ['dodges punch','catches fist','moves out of the way']
下面的代码将打印“punch”:
def bang(atk, atkL):
for i in atk:
if i in atkL:
print i
但下面的代码不会从punch打印随机元素:
def bang(atk, atkL):
for i in atk:
if i in atkL:
print (random.choice(i))
它不断打印单词punch中的一个字母('p'或'u'或'n'或'c'或'h')。你知道吗
此代码将从punch打印一个随机元素:
print (random.choice(punch))
如何将迭代器值添加到随机选择函数,以便打印冲压中的随机元素?你知道吗
def bang(atk, atkL):
for i in atk:
if i in atkL:
print (random.choice(i))
使用此代码,因为我认为您正在寻找类似随机输出的东西。你知道吗
但是
从非空序列seq返回一个随机元素。如果seq为空,则引发IndexError。你知道吗
我想你需要一本字典
然后你可以使用
i = "punch"
来获得随机穿孔或者
i = "kick"
得到随机踢相关问题 更多 >
编程相关推荐