我的程序有点问题。它根据所选角色随机生成属性。你知道吗
代码:
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()
用r=random.randrange随机范围内部HealthPoints和Power方法。你知道吗
当我运行它几次,它只是打印出生成的点是0。总是。你知道吗
怎么了?你知道吗
你的
self.name
总是空的,所以没有一个if
测试匹配。random.randrange
从不被调用,self.hp
和self.power
保持它们的类缺省值0
。你知道吗self.name
为空,因为getRandomPokemon_Powers
方法从未设置它:设置局部变量
name
,而不是self.name
。将该函数更改为:或者在其他地方设置实例的
name
属性:为什么不在主程序中打印
random.randrange
,然后在方法中打印r
,这样就可以知道它们是否是同一件事了?你知道吗即使有一些略显粗略的信息,您似乎也不太可能调用
random.randrange()
。这个测试将验证这个假设,然后您就可以开始寻找r
与其他东西绑定的地方了。直到你知道你在寻找什么,你可以期待所有你喜欢的没有太多希望的积极结果。你知道吗如果结果是相同的,那么我们需要看到更多的代码。你知道吗
最后一件事与你的问题有关:随着你的技能的发展,你会开始意识到仅仅编写代码并不总是最好的,因为一点点的想法可以节省你大量的打字,减少你的程序大小,使它更容易阅读(由有经验的程序员)。这里有一些(未经测试的)代码让你思考。你知道吗
编辑:要么问题被编辑了,要么代码比我在制定答案时看到的要多。你知道吗
相关问题 更多 >
编程相关推荐