1

I'm new to python and learning about classes and objects.

I have a file with lots of Pokemon data in csv format - example below:

Number,Name,Type1,Type2,HP,Attack,Defense,SpecialAtk,SpecialDef,Speed,Generation,Legendary,Mega
1,Bulbasaur,Grass,Poison,45,49,49,65,65,45,1,FALSE,FALSE
2,Ivysaur,Grass,Poison,60,62,63,80,80,60,1,FALSE,FALSE
3,Venusaur,Grass,Poison,80,82,83,100,100,80,1,FALSE,FALSE
6,Mega Charizard Y,Fire,Flying,78,104,78,159,115,100,1,FALSE,TRUE
10,Caterpie,Bug,,45,30,35,20,20,45,1,FALSE,FALSE
11,Metapod,Bug,,50,20,55,25,25,30,1,FALSE,FALSE
12,Butterfree,Bug,Flying,60,45,50,90,80,70,1,FALSE,FALSE
13,Weedle,Bug,Poison,40,35,30,20,20,50,1,FALSE,FALSE
14,Kakuna,Bug,Poison,45,25,50,25,25,35,1,FALSE,FALSE
20,Raticate,Normal,,55,81,60,50,70,97,1,FALSE,FALSE

I've defined my Pokemon class and opened the file below:

pokedex = open('../resource/lib/public/pokedex.csv', 'r')
for line in pokedex:
    row = line.strip().split(",")

class Pokemon:
    def __init__(self, Number, Name, Type1, Type2 = "" , HP, Attack, Defense,
                SpecialAtk, SpecialDef, Speed,Generation, Legendary, Mega):

        self.Number = Number
        self.Name = Name
        self.Type1 = Type1
        self.Type2 = Type2
        self.HP = HP
        self.Attack = Attack
        self.Defense = Defense
        self.SpecialAtk = SpecialAtk
        self.SpecialDef = SpecialDef
        self.Speed = Speed
        self.Generation = Generation
        self.Legendary = Legendary
        self.Mega = Mega

    def total_stats(self):
        total = self.HP+self.Attack+self.Defense+self.SpecialAtk+self.SpecialDef+self.Speed
        return total

I want to answer a series of questions using this data such as:

- What Pokemon has the highest HP statistic?
- Excluding Pokemon that are either Mega or Legendary, what Pokemon has the highest Defense statistic?
- Among Legendary Pokemon, what is the most common type? Include both Type1 and Type2 in your count.
-In terms of the sum of all six stats (HP, Attack, Defense, Special Attack, Special Defense, and Speed), what is the weakest Legendary Pokemon? If there is a tie, list any of the tying Pokemon.

How would I go about doing this? I am lost on how to link the data from file with the pokemon class. Please help!

moe92
  • 107
  • 7
  • Do you mean that you want to create AI to answer question ? Or you want to make quiz game ? – Philip Purwoko Jul 27 '20 at 03:46
  • No, I just want to be able to answer some questions by reading file data into class objects. – moe92 Jul 27 '20 at 03:51
  • For instance, I could use a regular loop on file open to answer questions. For example, I could do if row[0] == "Bulbasaur", return row[4] to get Bulbasaur's HP. But I want to do this with class and objects. – moe92 Jul 27 '20 at 03:54

2 Answers2

1

You can store the data in a list. This isn't something new. So, you'd do something like this

class Pokemon:
    # Read more about the adjustment made by removing the default value in Type2 - https://stackoverflow.com/a/48370634/5675325
    def __init__(self, Number, Name, Type1, Type2, HP, Attack, Defense, SpecialAtk, SpecialDef, Speed, Generation, Legendary, Mega):
        self.Number = Number
        self.Name = Name
        self.Type1 = Type1
        self.Type2 = Type2
        self.HP = HP
        self.Attack = Attack
        self.Defense = Defense
        self.SpecialAtk = SpecialAtk
        self.SpecialDef = SpecialDef
        self.Speed = Speed
        self.Generation = Generation
        self.Legendary = Legendary
        self.Mega = Mega

pokemon_list = []

with open('pokemon.csv', newline='') as csv_file:
    #reader = csv.reader(csv_file)
    #next(reader, None)
    results = []
    for line in csv_file:
        words = line.split(',')
        results.append((words[0:]))
    print(results)
    for Number, Name, Type1, Type2, HP, Attack, Defense, SpecialAtk, SpecialDef, Speed, Generation, Legendary, Mega in results:
        pokemon_list.append(Pokemon(Number, Name, Type1, Type2, HP, Attack, Defense, SpecialAtk, SpecialDef, Speed, Generation, Legendary, Mega))

pokemon_list.pop(0) # To remove the object created using headers

print(pokemon_list)

# Then do something with the pokemon_list.

In the console we'll see something like this

Console after execution

If we want to see what's pokemon_list variable

Pokemon list

Clicking for instances in the list item with index 3

Pokemon

As you mention to want the Pokemon with the highest HP, you'd do something like this using operator.attrgetter() to get that value

from operator import attrgetter

max_HP = max(pokemon_list, key=attrgetter('HP')).HP
max_ind = [obj for obj in pokemon_list if obj.HP == max_HP]

If we then adjust the code to include this part

from operator import attrgetter

class Pokemon:
    # Read more about the adjustment made by removing the default value in Type2 - https://stackoverflow.com/a/48370634/5675325
    def __init__(self, Number, Name, Type1, Type2, HP, Attack, Defense, SpecialAtk, SpecialDef, Speed, Generation, Legendary, Mega):
        self.Number = Number
        self.Name = Name
        self.Type1 = Type1
        self.Type2 = Type2
        self.HP = HP
        self.Attack = Attack
        self.Defense = Defense
        self.SpecialAtk = SpecialAtk
        self.SpecialDef = SpecialDef
        self.Speed = Speed
        self.Generation = Generation
        self.Legendary = Legendary
        self.Mega = Mega

pokemon_list = []

with open('pokemon.csv', newline='') as csv_file:
    #reader = csv.reader(csv_file)
    #next(reader, None)
    results = []
    for line in csv_file:
        words = line.split(',')
        results.append((words[0:]))
    #print(results)
    for Number, Name, Type1, Type2, HP, Attack, Defense, SpecialAtk, SpecialDef, Speed, Generation, Legendary, Mega in results:
        pokemon_list.append(Pokemon(Number, Name, Type1, Type2, HP, Attack, Defense, SpecialAtk, SpecialDef, Speed, Generation, Legendary, Mega))

pokemon_list.pop(0)

#print(pokemon_list)

max_HP = max(pokemon_list, key=attrgetter('HP')).HP
max_ind = [obj for obj in pokemon_list if obj.HP == max_HP]

print(max_ind)

We'll get the following in the console

Console after code adjustment

So we can see the list max_ind has only one value

Highest HP Pokemon

and that the max_HP is 80 and the Pokemon that has it is Venusaur.

To exclude Pokemon that are either Mega or Legendary, take inspiration from here and combine it with what was previously mentioned.

  • Apparently I cannot use csv in this problem, have to use regular file open. – moe92 Jul 27 '20 at 07:27
  • @moe92 just improved based on that change. In case you're wondering, I'm using Spyder (Python 3.7). – Tiago Martins Peres 李大仁 Jul 27 '20 at 08:17
  • 1
    Thanks so much. I was finally able to make it work. I unpacked file data into pokemon instances and stored them in pokemon_list using a loop and list indices after I split the string. Then I proceeded with using lists, dictionaries and loops to answer the rest of the questions and I got them all. The overall code was long and messy though but fine for my purposes. Didn't quite figure out the operator and attrgetter and it hasn't been taught to me in my MOOC, so I opted not to use them. – moe92 Jul 27 '20 at 13:10
0

You can do by create an instance

bulbasaur = Pokemon(1,'Bulbasaur','Grass','Poison',45,49,49,65,65,45,1,FALSE,FALSE 2)
Philip Purwoko
  • 316
  • 2
  • 10