Reputation: 33
I have been following the python course on udemy by Jose Portilla, and for one of our projects, we have to create a text-based Blackjack game. I have an error with a part of the code that looks like the following.
I have created a dictionary of values for the cards so I can easily see how much a card is worth (ex. If I have a King of Clubs, I want it to be equal to 10).
rankValues = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}
I then have a class that deals with the player hand. I had trouble with this part, so the solution looks like this.
class Hand:
def __init__(self):
self.cards = [] # start with an empty list as we did in the Deck class
self.value = 0 # start with zero value
self.aces = 0 # add an attribute to keep track of aces
def add_card(self,card):
self.cards.append(card)
self.value += rankValues[card.rank] #Where I need help!#
if card.rank == 'Ace':
self.aces += 1 # add to self.aces
def adjust_for_ace(self):
while self.value > 21 and self.aces:
self.value -= 10
self.aces -= 1
I don't understand this line at all. Help would be extremely appreciated!
Edit - Full Code
import random
import time
suits = ['Clubs', 'Spades', 'Diamonds', 'Hearts']
ranks = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace']
rankValues = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}
class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
def __str__(self):
return(f'{self.rank} of {self.suit}')
class Deck:
def __init__(self):
self.deck = []
for suit in suits:
for rank in ranks:
self.deck.append(Card(rank, suit))
def __str__(self):
comp = ''
for card in self.deck:
comp += '\n' + card.__str__()
return comp
def shuffle(self):
random.shuffle(self.deck)
def deal(self):
single_card = self.deck.pop()
return single_card
class Chips:
def __init__(self):
self.chips = 100
def win_bet(self, opponent_bet):
self.chips += opponent_bet
def lose_bet(self, my_bet):
self.chips -= my_bet
def __str__(self):
return(str(self.chips))
class Hand:
def __init__(self):
self.cards = []
self.points = 0
self.aces = 0
def add_card(self, card):
self.cards.append(card)
self.points += rankValues[card.rank] #error
def adjust_for_aces(self):
if self.points > 21 and self.aces:
self.aces -= 1
self.points -= 10
def __str__(self):
strversion = ' '
for card in self.cards:
strversion.append(card)
Upvotes: 2
Views: 484
Reputation: 5756
After the EDIT, it is still unclear what you don't understand. You have a dictionary with the values of the cards defined in a class card:
class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank
rankValues = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}
Example:
c1 = Card("Clubs", "Queen")
# To find the value, you need to look at what a Queen is worth:
rankValues["Queen"] # Returns 10. The key if place between [].
# Same as:
rankValues[c1.rank] # Because the attribute rank of c1 is "Queen"
Now the Hand
:
class Hand:
# Init an empty hand
def __init__(self):
self.cards = []
self.points = 0
self.aces = 0
# Add an object card to the hand
def add_card(self, card):
self.cards.append(card)
self.points += rankValues[card.rank]
Example with c1
:
my_hand = Hand()
my_hand.add_card(c1) # Enters the method add_card()
# What it does:
# self.cards.append(card) => my_hand.cards is a list() and will have the card c1 appended.
# self.points += rankValues[card.rank]
# => self.points is a value that will be incremented by the value of the card added.
# The value is given by the dictionnary rankValues, and is fetch by the rank of the card as the key.
Upvotes: 1