Aug-23-2022, 07:39 PM
(This post was last modified: Aug-23-2022, 08:02 PM by Yoriz.
Edit Reason: Formatting
)
I'm a beginner python programmer, taking an online python class. I am creating a Pokemon game.
I'm stuggling with the next teaching instruction
So I think the above should be written into the Player.py file. I put it there but the code is not working. Please help me to get this working.
Player.py File
I'm stuggling with the next teaching instruction
Quote:Just as we wrote is_alive for Pokemon, it would be handy to return whether a player has any pokemon left! Here's how we do that:
def has_pokemon_left(self): if self.next_pokemon() != None: return True else: return FalseHowever, whenever you have if A return True else return False , you should always just return A. So our code should be rewritten like this:
def has_pokemon_left(self): return (self.next_pokemon() != None)Just like with our Pokemon, we want a nice way of printing out players!
Try to create a function inside your player class that will print out the following information when a player is printed:
"I'm player <player_name>, and I have <number_of_pokemon> pokemon"
This might look something like this when a player is printed out:
"I'm player Billy, and I have 4 pokemon".
As a reminder, we can use the following code to overwrite the default str function of a class.
def __str__(self): return "YOUR PLAYER STRING GOES HERE"
So I think the above should be written into the Player.py file. I put it there but the code is not working. Please help me to get this working.
Player.py File
class Player(): def __init__(self, name, pokemon): self.name = name self.pokemon = pokemon def next_pokemon(self): alive_pokemon = [p for p in self.pokemon if p.is_alive()] if alive_pokemon: return alive_pokemon[0] else: return None def has_pokemon_left(self): return (self.next_pokemon() != None)Pokemon Game
class Pokemon(): def __init__(self): self.name = "" self.health = 0 def attack(self, other_pokemon): raise NotImplementedError( "Sorry, all subclasses have to write their own attack function!") class Pichu(Pokemon): def __init__(self): self.name = "Pichu" self.health = 30 def attack(self, other_pokemon): other_pokemon.health -= 2 if other_pokemon.is_alive(): print("Pichu ZAPPED {}, dealing 2 damage".format( other_pokemon.name, other_pokemon.health)) else: print("Pichu ZAPPED {}, which faints".format(other_pokemon.name)) def is_alive(self): if self.health > 0: return True else: return False def __str__(self): return "{} (health: {})".format(self.name, self.health) class Dummy(Pokemon): def __init__(self): self.name = "training dummy" self.health = 10 def attack(self, other_pokemon): print ("training dummy just sat there") def is_alive(self): if self.health > 0: return True else: return False def __str__(self): return "{} (health: {})".format(self.name, self.health) class Pikachu(Pokemon): def __init__(self): self.name = "Pikachu" self.health = 50 def attack(self, other_pokemon): other_pokemon.health -= 5 if other_pokemon.is_alive(): print("Pikachu ZAPPED {}, dealing 5 damage ".format( other_pokemon.name, other_pokemon.health)) else: print("Pikachu ZAPPED {}, which faints".format(other_pokemon.name)) def is_alive(self): if self.health > 0: return True else: return False def __str__(self): return "{} (health: {})".format(self.name, self.health) Bobs_pokemons = [Pichu(), Dummy()] player1 = Player("Bob", Bobs_pokemons) Player.save(player1, "Bob.player") del player1 player1 = Player.load("Bob.player") print(player1) print(player1.has_pokemon_left()) print(player1.next_pokemon()) dummy = Dummy() pichu = Pichu() pikachu = Pikachu() print (pichu) #print (dummy) print (pikachu) pichu.attack(pikachu) pikachu.attack(pichu) print (pichu) #print (dummy) print (pikachu)