3: Class syntax

Instructions

Create a class called Team.

Inside the class, create a name property. Assign the value "Tampa Bay Buccaneers" to this property.

Create an instance of the Team class, and assign it to the variable bucs.

Print the name property of the bucs instance.


In [1]:
class Car():
    def __init__(self):
        self.color = "black"
        self.make = "honda"
        self.model = "accord"

black_honda_accord = Car()

print(black_honda_accord.color)


black

Answer


In [2]:
class Team():
    def __init__(self):
        self.name = "Tampa Bay Buccaneers"
        
bucs = Team()

print(bucs.name)


Tampa Bay Buccaneers

4: Instance methods and init

Instrutions

Add a name parameter to the init method, and set the self.name property to the name argument.

Make an instance of the class, passing in the value "New York Giants" to the init function (when you write Team()).

Assign the result to the variable giants.

Answer


In [4]:
class Team():
    def __init__(self, name):
        self.name = name
        
giants = Team("New York Giants")

print(giants.name)


New York Giants

6: More instance methods

Instructions

Add an instance method called count_total_wins to the Team class. The method should take no arguments (except self), and should return the number of games the team won from 2009-2013.

Use the instance method to assign the number of wins by the "Denver Broncos" to broncos_wins.

Use the instance method to assign the number of wins by the "Kansas City Chiefs" to chiefs_wins.


In [5]:
import csv

f = open("nfl.csv", 'r')
nfl = list(csv.reader(f))

# The nfl data is loaded into the nfl variable.
class Team():
    def __init__(self, name):
        self.name = name

    def print_name(self):
        print(self.name)
        
    # Your method goes here
    
    
bucs = Team("Tampa Bay Buccaneers")
bucs.print_name()


Tampa Bay Buccaneers

Answer


In [6]:
import csv

f = open("nfl.csv", 'r')
nfl = list(csv.reader(f))

# The nfl data is loaded into the nfl variable.
class Team():
    def __init__(self, name):
        self.name = name

    def print_name(self):
        print(self.name)
        
    # Your method goes here
    def count_total_wins(self):
        wins = 0
        for row in nfl:
            if row[2] == self.name:
                wins += 1
        return wins
    
    
bucs = Team("Tampa Bay Buccaneers")
bucs.print_name()

broncos_wins = Team("Denver Broncos").count_total_wins()
chiefs_wins = Team("Kansas City Chiefs").count_total_wins()

print(broncos_wins, chiefs_wins)


Tampa Bay Buccaneers
8 3

7: Adding to the init function

Instructions

Add code in the init method to read and store the nfl data in the self.nfl property. The data is stored in "nfl.csv".

Then alter the code in the count_total_wins method to use the self.nfl property instead of the nfl variable (which no longer exists).

Use the instance method to assign the number of wins by the "Jacksonville Jaguars" to jaguars_wins.


In [10]:
import csv
class Team():
    def __init__(self, name):
        self.name = name
        f = open("nfl.csv", 'r')
        self.nfl = list(csv.reader(f))
        
    def count_total_wins(self):
        count = 0
        for row in self.nfl:
            if row[2] == self.name:
                count = count + 1
        return count
jaguars_wins = Team("Jacksonville Jaguars").count_total_wins()

In [11]:
print(jaguars_wins)


7

8: Wins in a year

Instructions

Add a method to the Team class that computes how many wins the team had in a given year.

The count_wins_in_year method should take a year string as an argument (e.g. "2011"), and return the number of wins the team had in that year.

Use the instance method to assign the number of wins in "2013" by the "San Francisco 49ers" to niners_wins_2013.


In [12]:
import csv
class Team():
    def __init__(self, name):
        self.name = name
        f = open("nfl.csv", 'r')
        csvreader = csv.reader(f)
        self.nfl = list(csvreader)

    def count_total_wins(self):
        count = 0
        for row in self.nfl:
            if row[2] == self.name:
                count = count + 1
        return count
    
    def count_wins_in_a_year(self, year):
        count = 0
        for row in self.nfl:
            if row[0] == year and row[2] == self.name:
                count = count + 1
        return count      
    
niners_wins_2013 = Team("San Francisco 49ers").count_wins_in_a_year("2013")
print(niners_wins_2013)


0

In [ ]: