View Single Post
Old 07-21-2014, 07:18 AM   #5
Join Date: Dec 2003
Location: Sydney, Australia
...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:

class Player(object): def __init__(self, playerid, name, height, jump): self.playerid = playerid = 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:

class Team(object): def __init__(self, location, city, name, record, arena): self.location = location = city = name self.record = record self.arena = arena

Then I populate the classes with player and team attributes:
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.
Politics, n. Strife of interests masquerading as a contest of principles.
--Ambrose Bierce

Last edited by Groundhog : 07-21-2014 at 07:20 AM.
Groundhog is offline   Reply With Quote