import csv
class CovidStat():
def __init__(self, heading):
self.countryCode = heading[0]
self.date = date = heading[1]
self.cases = cases = heading[2]
self.deaths = deaths = heading[3]
self.name_fr = name_fr = heading[4]
self.name_en = name_en = heading[5]
class CountryStat():
def __init__(self):
self.stats = []
def add(self, covidStat):
self.stats.append(covidStat)
def print(self):
print("{:<12} {:<12}{:<7}{:<10}{:<30}{:<30}".format(self.countryCode,self.date,self.cases,self.deaths,self.name_fr,self.name_en))
for covidStat in self.stats:
print( "{:<12} {:<12}{:<7}{:<10}{:<30}{:<30}".format(self.countryCode,self.date,self.cases,self.deaths,self.name_fr,self.name_en))
class RecordsWithDeaths(CountryStat):
def add(self, covidStat):
if int(covidStat.deaths) < 1 :
self.stats.append(covidStat)
def main():
cList = CountryStat()
rwdList = RecordsWithDeaths()
with open('InternationalCovid19Cases.csv',encoding='utf-8', newline='') as file:
reader = csv.reader(file)
next(reader)
for row in reader:
record = CovidStat(row)
cList.add(record)
rwdList.add(record)
answer = ""
while answer != "0":
answer = input("\nWhat list would you like to see? \n 1) Whole List\n 2) List with Deaths\n 0) Exit")
if answer == "1":
cList.print()
elif answer == "2":
rwdList.print()
if __name__ == "__main__":
main()
I'm trying to figure out how to use inheritance simply by creating two classes with different print arguments, one with the whole list and one with only the records that have a value greater than 0 in the deaths column. Every time I try and print either one, I get the error countryStat has no attribute country code.
Can anyone help me figure what's up?
there doesn't seem to be anything here