Random.choice不起作用

时间:2016-12-17 17:08:31

标签: python dictionary

我使用random.choice和字典,但它不起作用。此错误显示为 AttributeError:'功能'对象没有属性' choice'在main.py

的第90行

(事先制作课程)

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)

1 个答案:

答案 0 :(得分:1)

在代码的开头,您应该随机导入

import random

然后像这样调用它:

random.randint(something)

或:

random.choice(something)

或者如果您愿意,您只能导入randint和选择:

from random import randint,choice

然后你可以写:

randint(something)
choice(something)

如果您想了解有关随机模块的更多信息,请参阅文档:https://docs.python.org/3/library/random.html