【发布时间】:2016-12-17 17:08:31
【问题描述】:
我将 random.choice 与字典一起使用,但它不起作用。此错误显示为 AttributeError: 'function' 对象在 main.py 的第 90 行没有属性 'choice'
(提前做好课程)
items = {"Weak Leather Boots": Item("Weak Leather Boots", 1, "Armour"),"Health Potion":Item("Health Potion", 5, "Medicine" ), "Iron Fist":Item("Iron Fist" , 5, "Weapon"), "Speed Shoes":Item("Speed Shoes", 10, "Armour"),"Gold":randint(1, 50), "Ice Wand":Item("Ice Wand", 20, "Weapon") }
def Loot(lvl):
choose = random(items.values())
if choose.rarity > lvl:
choose = randint(items.values())
else:
print "You have found a " + choose.name
if type(choose) is int:
player.gold += choose
else:
player.inventory.append(choose.name)
【问题讨论】:
-
您粘贴的代码中似乎没有使用
random.choice。但是,第一个猜测是您在某处调用了函数random,并覆盖了对模块的引用 -
这个问题无法重现,因为你没有告诉我们你是如何导入
random模块或者你如何调用random.choice。您应该能够编写一个仅几行的小示例程序来演示该问题。
标签: python dictionary