View Question
Q: PYTHON - Selecting the correct Card and Suit - NOT SELECTING THE SUIT ( No Answer,   5 Comments )
 Question
 Subject: PYTHON - Selecting the correct Card and Suit - NOT SELECTING THE SUIT Category: Computers > Algorithms Asked by: macca1111-ga List Price: \$15.00 Posted: 10 Oct 2006 06:29 PDT Expires: 11 Oct 2006 19:43 PDT Question ID: 772237
 There is no answer at this time.

 Subject: Re: PYTHON - Selecting the correct Card and Suit - NOT SELECTING THE SUIT From: dmrmv-ga on 10 Oct 2006 09:56 PDT
 Note I'm not a GAR so fixing this is just for my amusement at lunchtime. I'm also not a Python expert so take this with a grain of salt:) You define the suits in a dictionary; this works in the manual example because you are calling the Card class correctly: Card(1,"s") with an integer index into the rank array and a key index into the suit dictionary. Your code snippet calls the Card class as though it is an array: Card(a,b) where a and b are integers, so for example the call might be Card(1,1) and the 1 isn't a valid key for the suit dictionary. The easiest fix is to just store the suits the same as the ranks: ["", "spades","diamonds","clubs", "hearts"] where the empty value is just a placeholder so your code doesn't have to access the 0th value. You could leave that out and just have the randrange function return an index from 0 to 3 (and you could do the same for the rank list). If you leave the suits stored as a dictionary you would have to add an extra step in where the key value is retrieved based on the integer supplied from the randrange step. Here is functional code that also prints the BJ value: from random import* #from Card import Card from string import* class Card: "A Card object is a numeric representation of a card." def __init__(self, rank, suit): "Indicates rank from 1-13, as well as the card's suit." self.rank = rank self.suit = suit self.ranks = [None, "ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"] self.suits = [None, "spades","diamonds","clubs","hearts"] self.BJ = [None, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10] def getRank(self): "Returns the rank of the card." return self.rank def getSuit(self): "Returns the suit of the card." return self.suit def BJValue(self): "Returns the Blackjack value of the card." return 0 ## temporary def __str__(self): #"Returns a string that names the card." return "%s of %s (%s)" % (self.ranks[self.rank], self.suits[self.suit], self.BJ[self.rank]) def main(): # Ask the user to select the number of cards n = input("Enter the number of cards to draw >>") for i in range(n): a = randrange(1,13) b = randrange(1,4) c=Card(a,b) print c main()
 Subject: Re: PYTHON - Selecting the correct Card and Suit - NOT SELECTING THE SUIT From: macca1111-ga on 10 Oct 2006 15:23 PDT
 Tks for the very quick response...
 Subject: Re: PYTHON - Selecting the correct Card and Suit - NOT SELECTING THE SUIT From: macca1111-ga on 10 Oct 2006 15:25 PDT
 When I work out how to complete the answer. You can have the payment...
 Subject: Re: PYTHON - Selecting the correct Card and Suit - NOT SELECTING THE SUIT From: macca1111-ga on 11 Oct 2006 04:05 PDT
 It works now. Just my fat fingers... Thank you
 Subject: Re: PYTHON - Selecting the correct Card and Suit - NOT SELECTING THE SUIT From: dmrmv-ga on 11 Oct 2006 06:31 PDT
 Glad it helped. Since I'm not a researcher my comment won't cost you anything but I feel somewhat badly that maniac who is a GAR showed interest after I put my comment up. Perhaps you could ask a supplementary question that maniac could respond to and earn your fee. Apologies to maniac as the fee posted was significant.