473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Error to be resolved

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
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>
Oct 26 '06 #1
4 1125
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>

Oct 26 '06 #2
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
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Oct 26 '06 #3
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 'o****@xiludom.gro'.split('@')])"
Oct 26 '06 #4
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>

Oct 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: dean | last post by:
Hello Group: I obtained a copy ofthe python 2.3.3 depot (binary zipped) from the hp porting archive. I have successfully unzipped the file and installed it using swinstall. I received the...
1
by: Phil Campaigne | last post by:
Hi All, Took a break from developing on pgsql 7.3 to set up network printing where I added another host(localhost 193.168.1.2) in red hat network settings. Later, I could start postmaster but...
5
by: Michael Kennedy [UB] | last post by:
Hi, I would like to report some weird behavior which results in an internal compiler error in VS.NET 2003 (VC++). I have an ATL COM dll project which also uses MFC. This solution (workspace...
7
by: wmkew | last post by:
Hello everyone I'm encountering a R6002 Runtime error and several bugs when trying to generate a simple Managed C++ application with .NET 2003. The main problem seems to arise from linking with...
2
by: S.K. Dutta | last post by:
Hi! I am having a problem that I do not have any clue on what to do. I have tries a few obvious things... <system.diagnostics> in the main web.config file is giving configuration error on a...
13
by: Praveen_db2 | last post by:
Hi All db2 8.1.3 Windows I am getting this error in db2diag.log.Please tell why is this error (-444) always there and how to go about for removing it. Diag.log output:...
0
by: RKM | last post by:
Hi All Following error is coming in particular web application during loading / login process. background----- we have 3 ip segment 172.26.1.x 172.26.2.x 172.26.3.x.
4
by: freeflytim | last post by:
I'm trying to implement a custom MembershipProvider (and RoleProvider) together with a custom MembershipUser class in C#, Asp.Net 2.0, MS Visual Studio 2005. Everything has worked fine so far,...
3
by: amanjsingh | last post by:
Hi, I am trying to implement Java Web Service using Apache Axis2 and Eclipse as a tool. I have created the basic code and deployed the service using various eclipse plugin but when I try to invoke...
3
by: pauldepstein | last post by:
I am using the latest version of Visual c++ on windows XP. I am trying to build a project and get a lnk1104 error regarding a file of the form .../../ ProjectName.lib No other errors, just this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.