Hi all. Please edit/add/help me make this blackjack program work. Thank you.

class Card:
    def __init__(self, suit, rank):
        self.rank = rank
        self.suit = suit
        if self.rank == 1:
            self.rank = "Ace"
            self.value = 11
        elif self.rank == 11:
            self.rank = "Jack"
            self.value = 10
        elif self.rank == 12:
            self.rank = "Queen"
            self.value = 10
        elif self.rank == 13:
            self.rank = "King"
            self.value = 10
        elif 2 <= self.rank <= 10:
            self.rank = str(self.rank)
            self.value = self.rank

        if self.suit == 1:
            self.suit = "Clubs"
        if self.suit == 2:
            self.suit = "Spades"
        if self.suit == 3:
            self.suit = "Diamonds"
        if self.suit == 4:
            self.suit = "Hearts"
        self.cardname = self.rank + " of " + self.suit


deck = []
for suit in range(1,5):
    for rank in range(1,14):
        deck.append(Card(suit,rank)

class Player:
    def player_turn():
        hand = []
        for cards in range(0,2):
            a = random.choice(deck)
            hand.append(a)
            deck.remove(a)

        print "This is your hand:"
        for card in hand:
            print card.cardname,

        print "Would you like to hit or stay?"
        response= raw_input("Type either 'hit' or 'stay'")
        while response =="hit":
            card=random.choice(deck)
            hand.append(card)
            deck.remove(card)
            print "Your next card is", card.cardname
            sum = 0
            for i in hand:
                sum=sum+i.value
                if sum > 21:
                    break
            print "Would you like to hit or stay?"
            response = raw_input("Type either 'hit' or stay'")        


        sum=0
            for i in hand:
                sum=sum+i.value
            if sum > 21:
            return "You lose. You Busted", sum
        else:
            return "This is your score: ", sum

class Dealer:

    def computer_turn():
        dealerhand = []
        for cards in range(0,2):
            a = random.choice(deck)
            hand.append(a)
            deck.remove(a)
        sum = 0
        for i in dealerhand:
            sum = sum+i.value
        while sum < 17
            card = random.choice(deck)
            dealerhand.append(card)
            deck.remove(card)
            sum = sum+card.value
            if sum > 21:
                return "You won! The dealer busted"
        return sum

Recommended Answers

All 5 Replies

For starters, you can use a dictionary and a list in the Card class. Test each class and function individually, whether it is in a class or not, before you include it your main program so you know that it works properly and returns the correct value. You obviously have too large of a program here for you to be able to debug.

class Card:
    def __init__(self, suit, rank):
        self.card_dict = {}
        self.card_dict[1]  = ["Ace", 11]
        self.card_dict[11] = ["Jack", 10]
        self.card_dict[12] = ["Queen", 10]
        self.card_dict[13] = ["King", 10]
       
        self.suit_list = ["*", "Clubs", "Spades",
                              "Diamonds", "Hearts" ]

    def rank_suit(self, suit, rank):
        if rank in self.card_dict:
            self.value = self.card_dict[rank][1]
            self.cardname = "%s of %s" % \
                 (self.card_dict[rank][0], self.suit_list[suit])              
        else: 
            self.value = self.rank
            self.cardname = "%s of %s" % \
                 (str(rank), self.suit_list[suit])

Like BearofNH pointed out in your other thread, the problem is in lines 18 and 19
self.rank = str(self.rank)
self.value = self.rank

You are turning self.rank into a string and then assign it self.value. The solution would be to simple reverse the two lines ...
self.value = self.rank
self.rank = str(self.rank)

Ok I am ALMOST DONE with the blackjack program, but there are these two bugs that I dont understand at all!

BUG #1:
For some reason, the program will only let you hit twice. I think I set up the while statement right, and I've checked it and I think it makes sense, so what is the problem?

Bug#2:
Even if you bust, if the computer busts as well, it will always say you won. In real blackjack it shouldn't be like that. How can I fix that?

#We use this module to pick a random card from the deck.
import random
#We use this module to close the program once the player goes bankrupt.
import sys

#Makes a card and assigns suit. rank, and value.
class Card:
    def __init__(self, suit, rank, value):
        self.rank = rank
        self.suit = suit
        self.value= value
        if self.rank == 1:
            self.rank = "Ace"
            self.value = 11
        elif self.rank == 11:
            self.rank = "Jack"
            self.value = 10
        elif self.rank == 12:
            self.rank = "Queen"
            self.value = 10
        elif self.rank == 13:
            self.rank = "King"
            self.value = 10
        elif 2 <= self.rank <= 10:
            self.value = self.rank
            self.rank = str(self.rank)
        if self.suit == 1:
            self.suit = "Clubs"
        if self.suit == 2:
            self.suit = "Spades"
        if self.suit == 3:
            self.suit = "Diamonds"
        if self.suit == 4:
            self.suit = "Hearts"
        self.cardname = self.rank + " of " + self.suit
#Makes a deck of cards
deck = []
for suit in range(1,5):
    for rank in range(1,14):
        for value in range(2,12):
            deck.append(Card(suit,rank,value))
#Creates a Player
class Player:
      
#Makes a function so the player can play the game.  
    def player_turn(self):
#Gives player two cards from the deck.
        self.hand = []
        for cards in range(0,2):
            a = random.choice(deck)
            self.hand.append(a)
            deck.remove(a)
#Prints the cards in hand.
        print "Here's your hand bud:"
        for card in self.hand:
            print card.cardname
       
        print "Would you like to hit or stay?"
        response= raw_input("Type either 'hit' or 'stay'")
        while response.lower()!= "hit" and response.lower()!="stay":
            response=raw_input("Type either 'hit' or 'stay'")
#Gives the player another card if he/she decides to hit.      
        while response =="hit" or response == "Hit":
            card=random.choice(deck)
            self.hand.append(card)
            deck.remove(card)
            print "Here's your next card:", card.cardname
            psum=0

#Adds up the value of each card in the player's hand.
            for i in self.hand:
                psum=int(i.value) + psum
#Makes the Ace have a value of 1, if the sum of the player's cards are greater than 21.
            for i in self.hand:
                if i.rank == 1 and p.Sum() > 21:
                    i.value = 1
                    
            if psum > 21:
                break
            print "Would you like to hit or stay?"
            response = raw_input("Type either 'hit' or 'stay'")
            while response.lower()!="hit" and response.lower()!="stay":
                response = raw_input("Type either 'hit' or 'stay'")
            return psum
          
      
#Adds the values of each card in the player's hand.  
    def Sum(self):
        self.sum=0
        for i in self.hand:
            self.sum= int(i.value)+self.sum
        return self.sum
  
  
      

#Makes a dealer class.
class Dealer:
#(Same as the Player)
    def computer_turn(self):
        self.dealerhand = []
        for cards in range(0,2):
            a = random.choice(deck)
            self.dealerhand.append(a)
            deck.remove(a)
        dsum = 0
        for i in self.dealerhand:
            dsum = int(i.value)+dsum
#Tells the dealer to hit if the dealer's score is less than 17.
        while dsum < 17:
            card = random.choice(deck)
            self.dealerhand.append(card)
            deck.remove(card)
            dsum = int(card.value)+dsum

        return dsum
  
    def Sum(self):
        self.sum=0
        for i in self.dealerhand:
            self.sum= int(i.value)+self.sum
        return self.sum


      
  
#Actual game      
print "Would you like to play Blackjack, the best game in the whole wide world!"
answer = raw_input("'Yes' or 'No'")

#Makes sure the player types in either yes or no.(It can be typed in either capital or lowercase letters.)
while answer.lower()!="yes" and answer.lower()!="no":
    answer = raw_input("'Yes' or 'No'")
#Quits out of the game if the player doesn't want to play.
if answer == 'No' or answer == 'no':
    sys.exit()
#Gives the player a starting amount of 100 chips to bet with.
total=100
while answer == "Yes" or answer == "yes":
#Creates a player and dealer object.
    p = Player()
    d = Dealer()
    print "You have", total, "chips"
#Allows the player to place a bet.
    betting=raw_input("How many chips would you like to bet?")
#Runs through both functions
    p.player_turn()
    d.computer_turn()
   
#The conditions under which the player can win or lose.   
    if p.Sum() > d.Sum() and p.Sum() <= 21:
        print "Awww shucks! You beat me. This was my score: "
        print d.Sum()
#Increases chip count by amount placed in bet.
        total=int(total)+int(betting)
        print "You now have", total, "chips"
        
    elif p.Sum()>21:
        print "You busted! Hahaha!"
        print d.Sum()
#Decreases chip count by amount placed in bet.
        total=int(total)-int(betting)
        print "You now have", total, "chips"
   
    elif d.Sum()>21:
        print "Shoot! Shoot! Shoot! I busted! Damn it, you won."
        print "This was my score: "
        print d.Sum()
        total=int(total)+int(betting)
        print "You now have", total, "chips"
        
    elif p.Sum() == d.Sum():
        print "A draw."

    else:
        print "Ahahaha! Feel the wrath. I just beat you, you lil punk. Your score was", p.Sum(), "My score was:"
        print d.Sum()
        total=int(total)-int(betting)
        print "You now have", total, "chips"
        
#Tells the player he/she is bankrupt and ends the game.      
    if total==0:
        print "YOU ARE BROKE! LOLOLOLOL!!!"
        sys.exit()
#Restarts the  game      
    print "Let's play again, so I can win all of your chips!...Would you like to play again?"
    answer = raw_input("'Yes' or 'No'")
    while answer.lower()!="yes" and answer.lower()!="no":
        answer = raw_input("'Yes' or 'No'")

    if answer == 'No' or answer == 'no':
        sys.exit()

In one of the "add up the cards" spots, print the cards as well. There is a problem with the way you access the class.

for i in self.hand:
                psum=int(i.value) + psum 
                print i.value, i.suit, i.rank

Or, add this print at the end of the class and then take a look at the deck loops. Print statements should be one of the first tools to use to determine what exactly the program is doing. Generally speaking, someone who has not tried this most basic of steps does not get much of a response.

self.cardname = self.rank + " of " + self.suit
        print self.cardname
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.