Google Answers Logo
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
Hi,

I have created a program that uses 2 random no.s to select a card. 
The first random no selects 1-13 including Kings etc.  The second
random number selects the suit ie 1-4.

Below is my Class and Script...

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 = {"s": "spades","d": "diamonds","c": "clubs","h": "hearts"}

    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" % (self.ranks[self.rank], self.suits.get(self.suit))

My script to print a card is...
from random import*
from Card import Card
from string import*
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()

It keeps printing out for example:  

4 of none
9 of none

were it should say:

4 or Spades - for example
9 of Heart - or whatever.

If I type directly into Python
x=Card(1,"s")
print x
i get the correct answer
'1 of Spades'

It also needs to print the BJ value of each card selected.

Request for Question Clarification by maniac-ga on 10 Oct 2006 11:13 PDT
Hello Macca1111,

If the comment is an adequate answer, I suggest you close the question
(so you are not charged further). If not, please indicate additional
information you need in a clarification. For example, if suits must be
in a dictionary - I can suggest a lookup method to convert your random
numbers to the index values (s, d, c, and h).

I can also make some additiona suggestions - such as revising the use
of randrange which as-is won't generate all possible card
combinations.

  --Maniac

Clarification of Question by macca1111-ga on 10 Oct 2006 15:22 PDT
Tks for the very quick response.  This has helped enormously...

Clarification of Question by macca1111-ga on 11 Oct 2006 03:38 PDT
It is still not working.  For some reason it won't read the suits when
I change it from a dictionary to a list???

I think it may be something to do with the __str__ def at the end of the class...

Any further help would be appreciated.

Request for Question Clarification by maniac-ga on 11 Oct 2006 08:34 PDT
Hello Macca1111,

As I mentioned before - if you are satisfied with the freely offered
comment - I suggest you close (or cancel) the question. An explanation
of this is included in the FAQ at
  https://answers.google.com/answers/faq.html#answervscomment
Good luck with your work and don't hesitate to return with your future questions.

  --Maniac
Answer  
There is no answer at this time.

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

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy