Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Error to be resolved

Reply
Thread Tools

Error to be resolved

 
 
Arun Nair
Guest
Posts: n/a
 
      10-26-2006
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
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>
>>> 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
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>


 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-26-2006
Arun Nair wrote:

> self.rank = rank
> self.rank = ["None","Clubs","Diamonds","Hearts","Spades"]


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

</F>



 
Reply With Quote
 
 
 
 
Arun Nair
Guest
Posts: n/a
 
      10-26-2006
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:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>> 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:\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
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-26-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 ''.split('@')])"
 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-26-2006
Arun Nair wrote:

> 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>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming using JSP and Tomcat: cannot be resolved to a type error Vincent Ly Java 14 12-03-2009 02:41 AM
org.apache.axis cannot be resolved to a type, Syntax error on token "enum", class expected dushkin Java 5 08-01-2007 04:34 PM
Custom MembershipProvider - WAT "Type not resolved" error freeflytim ASP .Net 4 05-16-2007 02:14 AM
Wireless XP SP2 netwk semi-resolved. =?Utf-8?B?VGluYS5W?= Wireless Networking 4 01-08-2005 02:05 AM
Remote Assistance fails to connect, remote remote host name could not be resolved Peter Sale Wireless Networking 1 12-11-2004 09:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57