Hey,
I'm having trouble with an exercise from a book, here it is;

" Write a program that reads the values for a random list of card from a file,(cards.txt) where each line in the file represents a single card with the rank and then suit seperated by a space. The rank should be an integer in the range 1 - 13 (Ace:1, King:13), while the suit should be lower case character (h, d, c, s). Sort the cards into suits ordered by rank, and print out the ordered list. Hint: sort the list first by rank, and then suit."

If anyone could give me a few pointers on how to do this exercise it would be greatly appreciated
Thanks
qwery__123

If I understand this right you want something similar to this ...

``````# take a random hand of cards where each card is identified by
# string 'rank suit' and sort by rank and suit

def cards_str2tup(cards):
"""
convert each card 'rank suit' string to a (suit, rank) tuple
where rank is an integer number
"""
cards_tup = []
for card in cards:
rank, suit = card.split()
cards_tup.append((suit, int(rank)))
return cards_tup

def cards_tup2str(cards_tup):
"""
convert each card (suit, rank) tuple to a 'rank suit' string
"""
cards = []
space = ' '
for tup in cards_tup:
suit, rank = tup
s = str(rank) + space + suit
cards.append(s)
return cards

# assume random hand of five cards
hand = ['4 c', '2 s', '12 h', '8 s', '6 s']

# this will sort looking at rank number as a string
hand.sort()
print hand  # oops  ['12 h', '2 s', '4 c', '6 s', '8 s']

# to sort properly convert to a list of (suit, int(rank)) tuples
# by default:
# suit, the first item in each tuple will be primary sort criteria
# rank, the second item in each tuple will be secondary sort criteria
cards_tup = cards_str2tup(hand)
cards_tup.sort()

# suits are in order and each suit's rank is in order too
print cards_tup  # [('c', 4), ('h', 12), ('s', 2), ('s', 6), ('s', 8)]

# convert the properly sorted list of tuples to a list of strings
cards = cards_tup2str(cards_tup)

print cards  # ['4 c', '12 h', '2 s', '6 s', '8 s']``````

Thanks for that mate, Now i understand where i was having trouble. i couldn't get my head around the classes. Just one more question, As the cards are sorted into a string, how do you go about placing them in a list .i.e. one card per line (placing newline char \n).

Here would be an example how to read the cards file into a list ...

``````"""
the cards.txt file looks something like this -->
4 c
10 d
12 s
6 d
9 c
12 d
4 h
9 s
7 d
8 d
3 c
3 h
13 h
4 s
...
...
"""

"""
read the shuffeled cards from a text file
that has one card string per line
"""
cards = []
for card in open(filename, 'r'):
# strip the trailing newline character
cards.append(card.strip())
return cards

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

# test
print cards  # ['4 c', '10 d', '12 s', '6 d', '9 c', ...]``````

Yeah sorry, i understand how to read a file into the program but the out put is returned in a sinlge line string eg./ etc...
what i ment to ask was how do you get this returned string (above) to print in list form,
ie.
6 h
7 h
8 h
9 h
and so on...

i hope i'm making sence to you

I think you are using wrong word here, list is like an array. You simply want each element of list displayed on separate line, right?

``````# for instance this is list
cards = ['4 c', '12 h', '2 s', '6 s', '8 s']

# one list item per line display
for card in cards:
print card

"""
my output =
4 c
12 h
2 s
6 s
8 s
"""``````

Edit: changed php tags, don't work properly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.