Random.randrange方法始终为0

时间:2014-03-05 22:21:01

标签: python random

我的程序遇到了一些麻烦。它会根据所选字符随机生成属性。

代码:

import random

not_e =     {"Bulbasaur": ["Overgrow", "Tackle", "Leech Seed"],
                  "Charmander": ["Blaze", "Scratchy", "Growl"],
                  "Squirtle": ["Torrent", "Tackle", "Tail Whip"],
                  "Pikachu": ["Static", "Tail Whip", "Thunder Shock"],
                  "Haunter": ["Levitate", "Hypnosis", "Spite"],}

evolved = {"Venusaur": ["Overgrow", "Vine Whip", "Tackle", "Growl", "Poison Powder"],
                   "Charizard": ["Blaze", "Growl", "Ember", "Dragon Rage", "Dragon Claw"],
                   "Blastoise": ["Torrent", "Tackle", "Water Gun", "Withdraw", "Bite"],
                   "Raichu": ["Static", "Thunderbolt", "Quick Attack", "Thunder Shock", "Electro Ball"],
                   "Gengar": ["Levitate", "Hypnosis", "Spite", "Curse", "Night Shade"]}



class YourPokemon(object):
    name = ""
    hp = 0 #health points
    power = 0 # the pokemons power
    wwyd = ["FIGHT", "CHANGE POKEMON", "OPEN BAG", "RUN"]
    pokemon_type = ""
    not_e_powers = []

    def getRandomPokemon_Powers(self):
        name = random.choice(list(not_e.keys())) # gets random pokemon
        not_e_powers = not_e[name] # gets list of powers from randomly selected pokemon
        print("Your pokemon is: " + name + "! And it's powers are: " + str(not_e_powers))
        return name, not_e_powers;

    def healthPointsAndPower(self):
        r = random.randrange
        if self.name == "Bulbasaur":
            self.hp = r(45,50)
            self.power = r(40,45)
            self.pokemon_type = "Grass"
        elif self.name == "Charmander":
            self.hp = r(35,40)
            self.power = (45,50)
            self.pokemon_type = "Fire"
        elif self.name == "Squirtle":
            self.hp = r(40,45)
            self.power = (45,50)
            self.pokemon_type = "Water"
        elif self.name == "Pikachu":
            self.hp = r(30,35)
            self.power = r(45,50)
            self.pokemon_type = "Electric"
        elif self.name == "Haunter":
            self.hp = r(40,45)
            self.power = r(45,50)
            self.pokemon_type = "Ghost"

        print("Your pokemons Health Points and Power Points are: ", self.hp, " and ", self.power, "!")





your_character = YourPokemon()

your_character.getRandomPokemon_Powers()
your_character.healthPointsAndPower()   

使用healthPointsAndPower方法中的r = random.randrange更新了代码。

当我多次运行它时,它会打印出生成的点数为0.一直都是。

它出了什么问题?

2 个答案:

答案 0 :(得分:1)

您的self.name始终为空,因此if个测试都不匹配。永远不会调用random.randrangeself.hpself.power保留其类默认值0

self.name为空,因为您的getRandomPokemon_Powers方法从不设置它:

name = random.choice(list(not_e.keys())) # gets random pokemon

设置本地变量name,而不是self.name。将该功能更改为:

def getRandomPokemon_Powers(self):
    self.name = random.choice(list(not_e.keys())) # gets random pokemon
    self.not_e_powers = not_e[name] # gets list of powers from randomly selected pokemon
    print("Your pokemon is: " + self.name + "! And it's powers are: " + str(self.not_e_powers))
    return self.name, self.not_e_powers

或在其他地方的实例上设置name属性:

>>> your_character.name = 'Bulbasaur'
>>> your_character.not_e_powers = ['Overgrow', 'Tackle', 'Leech Seed']
>>> your_character.healthPointsAndPower()
('Your pokemons Health Points and Power Points are: ', 48, ' and ', 40, '!')

答案 1 :(得分:1)

为什么不在主程序中打印random.randrange,然后在方法打印r中打印,这会告诉您它们是否相同?

即使提供稍微粗略的信息,您似乎也不太可能正在呼叫random.randrange()。此测试将验证该假设,然后您可以开始查找r与其他内容绑定的位置。在你知道自己在寻找什么之前,你可以看到所有你喜欢的东西,而没有太大的希望得到积极的结果。

如果事实证明它们是相同的,那么我们需要看到更多的代码。

与你的问题真正相关的最后一件事:随着你的技能发展,你将开始意识到,只是想出代码并不总是最好的,因为一点点思考可以为你节省很多打字并减少你的程序大小,使其更容易阅读(由经验丰富的程序员)。这里有一些(未经测试的)代码可以让你思考。

pokemons = {
    "Bulbasaur": (45, 50, 40, 45, "Grass"),
    "Charmander": (35, 40, 45, 50, "Fire"),
    "Squirtle": (40, 45, 45, 50, "Water"),
    "Pikachu": (30, 35, 45, 50, "Electric"),
    "Haunter": (40, 45, 45, 50, "Ghost")
}
...

def healthPointsAndPower(self):
    hp_lo, hp_hi, pow_lo, pow_hi, ptype = pokemons[self.name]
    sef.hp = r(hp_lo, hp_hi)
    self.power = r(pow_lo, pow_hi)
    self.pokemon_type = ptype

编辑:问题已被编辑,或者编写的代码多于我在制定答案时所看到的代码。

相关问题