[Tutor] basic class loading question

Dave Angel d at davea.name
Tue Nov 22 17:26:03 CET 2011


On 11/22/2011 09:20 AM, Cranky Frankie wrote:
> OK, but this is still not working:
>
> class Qb:
>      def __init__(self, first_name='', last_name='', phone='',
> email='', stadium=''):
>          self.first_name = first_name
>          self.last_name = last_name
>          self.phone = phone
>          self.email = email
>          self.stadium = stadium
>
>
>
> Qb_list = [["Joe", "Montana", "415-123-4567",
> "joe.montana at gmail.com","Candlestick Park"],
>      ["Fran", "Tarkington","651-321-7657",
> "frank.tarkington at gmail.com", "Metropolitan Stadidum"],
>      ["Joe", "Namath", "212-222-7777", "joe.namath at gmail.com", "Shea Stadium"],
>      ["John", "Elway", "303-9876-333", "john.elway at gmai.com", "Mile
> High Stadium"],
>      ["Archie", "Manning", "504-888-1234", "archie.manning at gmail.com",
> "Louisiana Superdome"],
>      ["Roger", "Staubach", "214-765-8989", "roger.staubach at gmail.com",
> "Cowboy Stadium"]]
>
>
>
> len_Qb_list = len(Qb_list)
>
> for i in range(0, len_Qb_list):
>      quarterbacks = Qb(*Qb_list[i])
>      i = i + 1
>
> print(quarterbacks.last_name(2))
>
>
You'll generally get better responses by saying in what way it's not 
working.  In this case, I get an exception in the print statement at the 
end.  So your message ought to state that, and show the traceback.

The specific error that causes that exception is you're trying to call a 
string.  What's that (2) all about anyway?   quarterbacks is an objext 
of type Qb, and quarterbacks.last_name is a string.

Your variable quarterbacks currently contains an object representing the 
last line of the list, the one for Roger Staubach.   You kept replacing 
the value in quarterbacks with the next item from the original list.  If 
you actually want a list of the objects, you need to say so.

quarterbacks = []
for ....
      quarterbacks.append(       )


Now that you really have a list, then you can print a particular one with:

print (quarterbacks[2].last_name)

(I tested my own variant using python 2.7.   And i'm not saying your 
code can't be improved.  You're learning, and getting it correct is much 
more important than getting it "optimal.")

-- 

DaveA



More information about the Tutor mailing list