Python Forum
Print Player Name and Number of Pokemon
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print Player Name and Number of Pokemon
#1
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)
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print two different sequence number mantonegro 2 1,629 Nov-16-2020, 06:19 PM
Last Post: mantonegro
  Print the number of items in a list on ubuntu terminal buttercup 2 1,897 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,679 Jun-18-2020, 04:59 PM
Last Post: QTPi
  Print 'X' a number of times Than999 1 2,611 Jan-18-2020, 06:41 PM
Last Post: Larz60+
  how do I make a number only print once but the number keeps on decreasing? syafiq14 5 2,888 Jan-03-2020, 10:34 AM
Last Post: perfringo
  print number of a list line per line lateublegende 2 2,684 Mar-20-2019, 04:07 PM
Last Post: lateublegende
  How to get number with print statement Rehan11 1 2,252 Jul-28-2018, 02:01 PM
Last Post: buran
  How to print number from user Rehan11 5 3,422 Dec-03-2017, 03:52 PM
Last Post: Larz60+
  print floating point number Regulus 19 10,995 Aug-14-2017, 02:42 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020