Error to be resolved

Bruno Desthuilliers onurb at xiludom.gro
Thu Oct 26 07:59:48 EDT 2006


Arun Nair wrote:
> Hey guys can you help me resolve this error
> 
> Thanks & Regards,
> 
> Arun Nair
> This is the program
> ========================================================================
> from random import *
> from string import *

Avoid the from XXX import * idiom whenever possible. Better to
explicitely tell what you intend to use, and where it comes from.

> class Card:

Make this

class Card(object):

>     def __init__(self, suit, rank):
>         self.suit = suit
>         self.rank = rank
=> here you assign rank to self.rank	
>         self.rank = ["None","Clubs","Diamonds","Hearts","Spades"]
=> and here you overwrite self.rank
>         self.suit = ["zero", "Ace", "2", "3", "4", "5", "6", "7", "8",
> "9", "10", "Jack", "Queen", "King"]
=> and here you overwrite self.suit

You have to use different names for the effective values of rank and
suit for a given instance of Card than for the lists of possible values
(hint : usually, one uses plural forms for collections - so the list of
possible ranks should be named 'ranks').

Also, since these lists of possible values are common to all cards,
you'd better define them as class attributes (ie: define them in the
class statement block but outside the __init__() method), so they'll be
shared by all Card instances.

<OT>
 While we're at it, I think that you inverted suits and ranks... Please
someone correct me if I'm wrong.
</OT>

>         self.BJ = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

Idem, this is a list of possible blackjack values, not the effective BJ
value for a given instance.

>     def getRank(self):
>         return self.rank
> 
>     def getSuit(self):
>         return self.suit
> 
>     def BJValue(self):
>         return self.BJ
This will return the whole list of BJ values, not the effective value
for the current Card instance

>     def __str__(self):
>         return " %s of %s(%s)" % (self.rank[self.rank],
> self.suit[self.suit], self.BJ[self.rank])

> def main():
>     n = input("How many cards do you want to draw from the deck?")

better to use raw_input() and validate/convert the user's inputs by
yourself.

(snip)
> =========================================================================
> This is the error
>>>> Traceback (most recent call last):
>   File
> "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
> line 310, in RunScript
>     exec codeObject in __main__.__dict__
>   File "D:\A2_3.1.py", line 32, in ?
>     main()
>   File "D:\A2_3.1.py", line 30, in main
>     print c
>   File "D:\A2_3.1.py", line 22, in __str__
>     return " %s of %s(%s)" % (self.rank[self.rank],
> self.suit[self.suit], self.BJ[self.rank])
> TypeError: list indices must be integers

of course. You're trying to use self.rank (which is now a list) as an
index to itself (dito for self.suit).

HTH
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list