Python - 将随机数分配给字典值?

时间:2013-11-30 16:10:09

标签: python function random dictionary range

我目前正尝试将随机值分配给字典。每当我调用* player [“enemy_hit”] *时,我希望它从我提供的范围中选择一个随机值。目前,它从该范围中为其分配单个值。有什么方法吗?谢谢!

import random

def easy_difficulty(player):
    player["enemy_hit"] = random.randrange(3, 10)
    return player

def player_main():
    player = {"health" : 100,
              "xp" : 100,
              "strength" : 0,
              "dexterity" : 0,
              "wisdom" : 0,
              "level_req" : None,
              "health_spawn" : None,
              "enemy_hit" : None}
    player = easy_difficulty(player)
    print(player["enemy_hit"])        
    print(player["enemy_hit"]) 
    print(player["enemy_hit"])       
    return player

player_main()

>>> 
3
3
3
>>> 

5 个答案:

答案 0 :(得分:2)

这是让Player成为一个类,enenmy_hit成为一个方法的主要示例。有关如何使用类和对象的更多信息,请参阅Python Tutorial section on classes

import random

class Player(object):

    def __init__(self, health=100, xp=100, strength=0, dexterity=0,
                 wisdom=0, level_req=None, difficulty=1.0):
        """Initialize the player's attributes (health, xp, ...).
        Create a player object with some default values, unless the have
        been specified as keyword arguments.
        """
        self.health = health
        self.xp = xp
        self.strength = strength
        self.dexterity = dexterity
        self.wisdom = wisdom
        self.level_req = level_req
        self.difficulty = difficulty

    def hit(self):
        """This is the method that calculates a hit on the player.
        It uses a difficulty factor `self.difficulty` set when the class
        was instanciated, and multiplies it by some random amount to determine
        the damage taken. That damage then is subtracted from the player's
        health.
        """
        damage_taken = random.randint(1, 10) * self.difficulty
        self.health -= damage_taken
        print "Taken %s damage, down to %s!" % (damage_taken, self.health)


def main():
    player = Player(difficulty=1.5)
    player.hit()
    player.hit()
    player.hit()

if __name__ == '__main__':
    main()

输出:

Taken 1.5 damage, down to 98.5!
Taken 13.5 damage, down to 85.0!
Taken 15.0 damage, down to 70.0!

通过做这样的事情

newbie = Player(difficulty=0.5)
veteran = Player(difficulty=2.0)
fighter = Player(strength=100, wisdom=10)
wizard = Player(strength=5, wisdom=70)

您可以利用创建具有不同属性的同一类的多个实例,在必要时覆盖Player.__init__()设置的默认值。

答案 1 :(得分:0)

将函数分配给player["enemy_hit"],每次调用时都会产生一个值,例如

player["enemy_hit"] = lambda: random.randrange(3, 10)

然后您可以像

一样访问它
player["enemy_hit"]()

它将产生一个随机值。

答案 2 :(得分:0)

您的easy_difficulty函数每次调用时都会为player["enemy_hit"]分配一个随机值。您只需拨打一次 - print来电之间没有任何变化。

您可能希望在每次easy_difficulty来电之前致电print

答案 3 :(得分:0)

您只执行一次easy_difficulty而只打印三次,试试这个:

player = easy_difficulty(player)
print(player["enemy_hit"])      
player = easy_difficulty(player)  
print(player["enemy_hit"]) 
player = easy_difficulty(player)
print(player["enemy_hit"])    

答案 4 :(得分:0)

因为你打印同样的东西3次。您需要在打印前再次致电player_difficulty