Python Forum
Print Player Name and Number of Pokemon - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Print Player Name and Number of Pokemon (/thread-38031.html)



Print Player Name and Number of Pokemon - webmanoffesto - Aug-23-2022

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
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 False
However, 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)



RE: Print Player Name and Number of Pokemon - deanhystad - Aug-23-2022

How is it not working? Does your program crash with an error message? If so, please post the error message. The code you've posted does not run. Your are missing Player.save() and Player.load() methods. When I remove the game code that saves and loads player 1, the program appears to work. This command:
print(player1.has_pokemon_left())
prints
Output:
True
next_pokemon() doesn't look right to me. I would expect that to return the next pokemon, not the first pokemon. Maybe writing an iterator that lets you loop through your pokemon is a future lesson.

This comment:
Quote:As a reminder, we can use the following code to overwrite the default str function of a class.
Is telling you wo write a __str__() method for your Player class so you get something better than:
Output:
<__main__.Player object at 0x0000022204EC1460>
when you print(player).

This comment:
Quote:However, whenever you have if A return True else return False , you should always just return A. So our code should be rewritten like this:
Is telling you to rewrite this:
    def is_alive(self):
        if self.health > 0:
            return True
        else:
            return False
I'm telling you that there is no need to have this method defined in each of your Pokemon subclasses. It should be in the Pokemon class, and Pikachu and others should inherit the method. Maybe that is also a later lesson.