我正在尝试在python中为RPG游戏制作一个3部分名称生成器。我有一个二维数组,以组合一个名称的3个部分:
name = [["Raan","Feim","Od","Yol","Dur"]["Mir","Zii","Ah","Toor","Neh"][ "Tah","Gron","Viing","Shul","Viir"]]
然而,当我运行它时,我收到错误:
TypeError: list indices must be integers or slices, not tuple
有没有解决方法/可以在二维数组中完成
答案 0 :(得分:2)
错误的列表语法。这有效:
import random
name = [["Raan","Feim","Od","Yol","Dur"],["Mir","Zii","Ah","Toor","Neh"],["Tah","Gron","Viing","Shul","Viir"]]
print (" ".join([random.choice(nl) for nl in name]))
答案 1 :(得分:2)
每个列表中都需要逗号。
name = [["Raan","Feim","Od","Yol","Dur"],["Mir","Zii","Ah","Toor","Neh"],[ "Tah","Gron","Viing","Shul","Viir"]]
然后使用随机名称:
from random import randint
print (name[randint(0,2)][randint(0,4)])
你可以这样做3次,或者你想要这个名字出来
<强>更新强>
这是我猜你想要的东西:
from random import randint
name = [["Raan","Feim","Od","Yol","Dur"],["Mir","Zii","Ah","Toor","Neh"],[ "Tah","Gron","Viing","Shul","Viir"]]
print(name[randint(0,2)][randint(0,4)] + name[randint(0,2)][randint(0,4)] + name[randint(0,2)][randint(0,4)])
答案 2 :(得分:1)
您的列表之间需要逗号:
name = [["Raan","Feim","Od","Yol","Dur"], ["Mir","Zii","Ah","Toor","Neh"], [ "Tah","Gron","Viing","Shul","Viir"]]