【发布时间】:2020-11-16 08:59:26
【问题描述】:
我是 python 新手,正在学习类和对象。
我有一个包含大量 csv 格式的 Pokemon 数据的文件 - 示例如下:
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
我已经定义了我的 Pokemon 类并打开了下面的文件:
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
我想使用这些数据回答一系列问题,例如:
- 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.
我该怎么做呢?我不知道如何将文件中的数据与口袋妖怪类链接起来。请帮忙!
【问题讨论】:
-
您的意思是要创建AI来回答问题吗?或者你想做问答游戏?
-
不,我只是想通过将文件数据读入类对象来回答一些问题。
-
例如,我可以在打开文件时使用常规循环来回答问题。例如,如果 row[0] == "Bulbasaur",我可以这样做,返回 row[4] 以获取 Bulbasaur 的 HP。但我想用类和对象来做到这一点。
标签: python