...or not.
I had a lot of trouble pulling individual list items out of dictionaries - it just didn't seem to be the right way to go about it. I found some info online, but for something that must be a very common part of most programs, there had to be an easier way to do this.
Instead of cheating and creating individual variables for each rating, I would instead read on to the next chapter and find my solution: the mystical realm of "classes".
This is where the book really began to fail me. I read the chapter multiple times but I still didn't feel like I had a grasp on what a class actually is. What separates it from a function? It seems obvious now, but it took a few google searches for me to grasp the concept, and suddenly everything became much clearer.
THIS is how you would store attributes for a player and then recall them as required. Although I don't yet know how, it also seems like it would be quite simple to pull these attributes from a database or file.
So I created a simple class that would be used to store player information:
Code:
class Player(object):
def __init__(self, playerid, name, height, jump):
self.playerid = playerid
self.name = name
self.height = height
self.jump = jump
This allows me to re-use this class as I keep going and add more attributes as required.
I then create a class to pull in the team information too - not really needed for this, but seemed simple enough and would make a neat little touch to the play-by-play:
Code:
class Team(object):
def __init__(self, location, city, name, record, arena):
self.location = location
self.city = city
self.name = name
self.record = record
self.arena = arena
Then I populate the classes with player and team attributes:
Code:
HomeTeam = Team(1, "Cleveland", "Cavaliers", "25-11", "Quicken Loans Arena")
AwayTeam = Team(2, "Charlotte", "Hornets", "17-16", "Time Warner Cable Arena")
PlayerHome5 = Player(1, "A. Varejao", 70, 10)
PlayerHome4 = Player(2, "T. Thompson", 68, 12)
PlayerAway5 = Player(14, "A. Jefferson", 70, 7)
PlayerAway4 = Player(15, "N. Vonleh", 69, 13)
This allows me to easily call any attribute by using PlayerAway5.height for example to then pass these values on to functions etc.