Error to be resolved

A

Arun Nair

Hey guys can you help me resolve this error

Thanks & Regards,

Arun Nair
This is the program
========================================================================
from random import *
from string import *
class Card:

def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
self.rank = ["None","Clubs","Diamonds","Hearts","Spades"]
self.suit = ["zero", "Ace", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King"]
self.BJ = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

def getRank(self):
return self.rank

def getSuit(self):
return self.suit

def BJValue(self):
return self.BJ

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?")
for i in range(n):
a = randrange(1,13)
b = randrange(1,4)
c = Card(a,b)
print c

main()
=========================================================================
This is the error 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
 
F

Fredrik Lundh

Arun said:
self.rank = rank
self.rank = ["None","Clubs","Diamonds","Hearts","Spades"]

hint: what's "self.rank" after you've executed the above?

</F>
 
A

Arun Nair

These is the latest with the changes made:
=============================================================================================
from random import *
from string import *
class Card:

def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
self.suit = ["None","Clubs","Diamonds","Hearts","Spades"]
self.rank = ["zero", "Ace", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "Jack", "Queen", "King"]
self.BJ = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]

def getRank(self):
return self.rank

def getSuit(self):
return self.suit

def BJValue(self):
return self.BJ

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

def main():
n = input("How many cards do you want to draw from the deck?")
for i in range(n):
a = randrange(1,13)
b = randrange(1,4)
c = Card(a,b)
print c

main()
===============================================================================================
Still get an error: File
"C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "D:\Charles Sturt University\ITC106
200670\Assignment2\A2_3.1.py", line 33, in ?
main()
File "D:\Charles Sturt University\ITC106
200670\Assignment2\A2_3.1.py", line 31, in main
print c
File "D:\Charles Sturt University\ITC106
200670\Assignment2\A2_3.1.py", line 23, in __str__
return self.rank + " of " + self.suit
TypeError: can only concatenate list (not "str") to list
 
B

Bruno Desthuilliers

Arun said:
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.
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 errorFile
"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
 
F

Fredrik Lundh

Arun said:
self.suit = suit
self.rank = rank
self.suit = ["None","Clubs","Diamonds","Hearts","Spades"]
self.rank = ["zero", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen",
"King"]

hint: what happens if two variables have the same name ? can Python magically figure
out which one you mean when you try to use one of them, or does something else
happen?
File "D:\Charles Sturt University\

could you perhaps ask your advisors to drop by and tell us why he/she expects us to
do their job ?

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top