Newbie help understanding...

M

mark

Hi I am trying to get a piece of code to work based on an exercise in
a book. Any help appreciated. Can someone please explain what is going
on here.

I am trying to read from a text file a list of cards with the
following format and sort firstly by suit and then by rank

h 1
d 2
c 5
s 9
h2
d3

etc...

I get the following error;
Traceback (most recent call last):
File "F:/###UNI###/ITC106/ass2/cardread.py", line 25, in <module>
t.append( t[0] + 400 )
AttributeError: 'str' object has no attribute 'append'

def read_cards(filename):
cards = []
for card in open(filename, 'r'):
cards.append(card.strip())
return cards

# read the deck of cards from a file
filename = 'cards.txt'
cards = read_cards(filename)


for t in read_cards(filename):
if t[1] == 'h':
t.append( t[0] + 100 )
elif t[1] == 'd':
t.append( t[0] + 200 )
elif t[1] == 'c':
t.append( t[0] + 300 )
else:
t.append( t[0] + 400 )



print read_cards(filename)

Regards


J
 
A

Amit Khemka

Hi I am trying to get a piece of code to work based on an exercise in
a book. Any help appreciated. Can someone please explain what is going
on here.

I am trying to read from a text file a list of cards with the
following format and sort firstly by suit and then by rank

h 1
d 2
c 5
s 9
h2
d3

etc...

I get the following error;
Traceback (most recent call last):
File "F:/###UNI###/ITC106/ass2/cardread.py", line 25, in <module>
t.append( t[0] + 400 )
AttributeError: 'str' object has no attribute 'append'
def read_cards(filename):
cards = []
for card in open(filename, 'r'):
cards.append(card.strip())
return cards

# read the deck of cards from a file
filename = 'cards.txt'
cards = read_cards(filename)


for t in read_cards(filename):
if t[1] == 'h':
t.append( t[0] + 100 )
elif t[1] == 'd':
t.append( t[0] + 200 )
elif t[1] == 'c':
t.append( t[0] + 300 )
else:
t.append( t[0] + 400 )

In read_cards function you are appending a string in the list
'cards' , where i guess you wanted to append a list of suit and rank
in list cards.

def read_cards(filename):
cards = []
for card in file(filename):
cards.append(cards.split()) # i assume that suit and rank is
separated by white space
return cards

or better still

cards = [card.split() for card in file(filename)]

Cheers,
 
C

cmpython

The problem is that in your function t is a string (one of the cards
in the list called "cards") and strings don't have the ability to use
the append method. But lists do. Therefore t.append is wrong but
cards.append works fine. (Append means "take the list you have and
add what is in the parenthesis to it...so cards.append("Ace") would
append the string Ace to the list of cards).
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top