1. #1

    Python Programming Halp <3

    So, I am really stuck on my programming assignment which I have been working on and cant seem to fix. If anyone can help guide me in the right direction (not the answer) it would be much appreciated. My professor is barely understandable and is practically useless to ask for help....




    from random import randrange

    class Card:

    def __init__(self,suit,rank):
    self.rank = rank
    self.suit = suit

    def GetRank(self):
    self.rank = randrange(1,13)
    return self.rank

    def GetSuit(self):
    self.suit = randrange(1,4)
    if (self.suit == 1):
    suit = "Clubs"
    elif(self.suit == 2):
    suit = "Diamonds"
    elif(self.suit == 3):
    suit = "Hearts"
    elif(self.suit == 4):
    suit = "Spades"
    return self.suit


    def BJValue(self):
    if (self.rank == 1):
    self.BJValue = 10
    elif (self.rank == 11,12,13):
    self.BJValue = 10
    else:
    self.BJValue = self.rank



    def PrintName(self):

    print(self.rank, "of ", self.suit)

    if (self.suit == "Spades"):
    if (self.rank == 1):
    print("Ace of Spades")
    elif (self.rank >= 2 and self.rank <= 10):
    print(str(self.rank)+" of Spades")
    elif (self.rank == 11):
    print("Jack of Spades")
    elif (self.rank == 12):
    print("Queen of Spades")
    else:
    print("King of Spades")

    if (self.suit == "Hearts"):
    if (self.rank == 1):
    print("Ace of Hearts")
    elif (self.rank >= 2 and self.rank <= 10):
    print(str(self.rank)+" of Hearts")
    elif (self.rank == 11):
    print("Jack of Hearts")
    elif (self.rank == 12):
    print("Queen of Hearts")
    else:
    print("King of Hearts")

    if (self.suit == "Diamonds"):
    if (self.rank == 1):
    print("Ace of Diamonds")
    elif (self.rank >= 2 and self.rank <= 10):
    print(str(self.rank)+" of Diamonds")
    elif (self.rank == 11):
    print("Jack of Diamonds")
    elif (self.rank == 12):
    print("Queen of Diamonds")
    else:
    print("King of Diamonds")

    if (self.suit == "Clubs"):
    if (self.rank == 1):
    print("Ace of Clubs")
    elif (self.rank >= 2 and self.rank <= 10):
    print(str(self.rank)+" of Clubs")
    elif (self.rank == 11):
    print("Jack of Clubs")
    elif (self.rank == 12):
    print("Queen of Clubs")
    else:
    print("King of Clubs")
    def main():


    print("Here's your hand:")
    print("----------------")
    card1 = 0
    card2 = 0
    card3 = 0
    card4 = 0
    card5 = 0
    card6 = 0
    card7 = 0

    for i in range(7):

    card1.PrintName()
    card1.BJValue()
    card2.BJValue()
    card2.PrintName()
    card3.BJValue()
    card3.PrintName()
    card4.BJValue()
    card4.PrintName()
    card5.BJValue()
    card5.PrintName()
    card6.BJValue()
    card6.PrintName()
    card7.BJValue()
    card7.PrintName()

    main()

    Error:
    File "pa9.py", line 142, in ?
    main()
    File "pa9.py", line 125, in main
    card1.PrintName()

    What output should be:
    Here's your hand:
    -----------------
    Jack of Spades
    5 of Clubs
    6 of Diamonds
    9 of Diamonds
    4 of Hearts
    8 of Spades
    King of Clubs

  2. #2
    stackoverflow.com is your friend for programming questions

  3. #3
    Deleted
    Moved to Computer (General), where you should hopefully find more help.

  4. #4
    Not sure if you got it to work or not, but the following should work (do note, I didn't optimize the code. Plus, it's been quite a few years since I have worked with Python)

    - Remove (unless you want to pass the variables in):

    def __init__(self,suit,rank):
    self.rank = rank
    self.suit = suit
    - In GetSuit(), it will return the number, not the text, to return the text, add the following line right before the return:

    self.suit = suit
    - Note that in PrintName it will show the card twice (once with the numbers above 10 and one with the names switched in, ie Ace, King, Queen, Jack).

    - Also, you can swap the following in for the loop, a little bit cleaner:

    for i in range(7):

    card = Card()
    card.GetRank()
    card.GetSuit()
    card.BJValue()
    card.PrintName()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •