import random
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
return f'{self.rank} of {self.suit}'
def __repr__(self):
return f'Card({self.rank!r}, {self.suit!r})'
class Deck:
suits = ['hearts', 'spades', 'clubs', 'diamonds']
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
def __init__(self):
self.cards = []
self.populate()
def populate(self):
for suit in Deck.suits:
for rank in Deck.ranks:
card = Card(rank, suit)
self.cards.append(card)
def shuffle(self):
random.shuffle(self.cards)
def deal(self):
return self.cards.pop()
class Hand:
def __init__(self):
self.cards = []
def add_card(self, card):
self.cards.append(card)
deck = Deck() # Make new deck
deck.shuffle() # Shuffle it
hand = Hand() # Make new hand
card = deck.deal() # Deal card from deck
hand.add_card(card) # Add that card to the hand
print(hand.cards) # Display what cards are in the hand right now
#๐ Working on blackjack to practice oop
134 messages ยท Page 1 of 1 (latest)
@toxic arrow
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
3rd post nice
because im always here ?
yes
im always staring at #1035199133436354600 / notifs of it
i have it give notification on post created as well
ohh thats nice
so what u need help with
idk whats next
- deal a card from the deck to a hand
- compare hands (highest wins?)
- catch a card which overflows a hand?
A players list which is a list of Hands?
yes this i think comes first
how about a function that has total amount of points
well u did all the game pieces havent u
now its piecing them together
u already pieced deck and card together
yes
now piece hand and deck together
what does that mean
maybe i was rushing to project too soon im starting to feel like
nah u doing great
here is another way to put it
we have our pieces
the deck the cards and the hand (which is essentially a player for blackjack)
now we make the actual blackjack game
using said pieces
well is that a blackjack feature
yes
then do we need to count it u think ? 
in the hand class
below add_cards
logically it makes sense to me but then again im stoopid
ye thats fine in our scenario dw
that im stoopid?
i meant adding it below add_cards in hand class
but sure that too
ahahh
if it makes u feel better
haahahhah
it would be less fine however if we were doing like, poker and blackjack in 1 application
no no
cause then why our poker hand is calculating its blackjack value instead of its poker value
or is it doing poker value
wait waht
and if its doing poker value then what about hand in blackjack
im confused
what am i supposed to do next?
ok
def get_value(self):
total = 0
if card.rank in ['J', 'Q', 'K']:
total += 10
if card.rank == "A":
total += 11
but
how would i make the ace either 1 or 11
or am i even doing this right
u choose one
ok 11 it is
there is no right way and no wrong way to do things in coding
there is simple and complicated/complex ways of doing things
but even then there is personal prefence bias playing a role
if there is no way to differentiate when u chould choose 1 or when u should choose 11 then u pick one value and roll with it
and example of being able to differentiate would be like a condition
if hand.value > 15: ace_value =1 else 11
for example
ohh right right
so either pick one of pick a random condition
its ur game version of blackjack after all

ok 11 it is
import random
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def __str__(self):
return f'{self.rank} of {self.suit}'
def __repr__(self):
return f'Card({self.rank!r}, {self.suit!r})'
class Deck:
suits = ['hearts', 'spades', 'clubs', 'diamonds']
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
def __init__(self):
self.cards = []
self.populate()
def populate(self):
for suit in Deck.suits:
for rank in Deck.ranks:
card = Card(rank, suit)
self.cards.append(card)
def shuffle(self):
random.shuffle(self.cards)
def deal(self):
return self.cards.pop()
class Hand:
def __init__(self):
self.cards = []
def add_card(self, card):
self.cards.append(card)
def get_value(self):
total = 0
if card.rank in ['J', 'Q', 'K']:
total += 10
if card.rank == "A":
total += 11
if total >= 11 and card.rank == "A":
total += 1
deck = Deck() # Make new deck
deck.shuffle() # Shuffle it
hand = Hand() # Make new hand
card = deck.deal() # Deal card from deck
hand.add_card(card) # Add that card to the hand
print(hand.cards) # Display what cards are in the hand right now
yeah somethings not it
if card.rank == "A":
total += 11
if total >= 11 and card.rank == "A":
total += 1
if card rank is A add 11
if ... and card rank is A: add 1
u got somewhat duplicated conditions

yeah
i already said it
either pick one and fuck the other
or pick a condition and use said condition to switch btw the 2
def get_value(self):
total = 0
if card.rank in ['J', 'Q', 'K']:
total += 10
if card.rank == "A":
total += 11
```
fuck it we roll
i dont like how i have 2 if's statments
well how many would u like to have
then what would i have for else?
u dont need else
if ___:
pass
elif ___:
pass
this is a perfectly valid if statement
nope
speaking of else
there is loads of other places u can use it
like try/except blocks
if u know those
i know of them
then there is the most random place imaginable
for loops
!e
for i in range(5):
print(i)
else:
print(':)')
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | 0
002 | 1
003 | 2
004 | 3
005 | 4
006 | :)
anyways thats very offtopic
so is this all u need u think ? 
im getting burnt out not knowing wtf to do man
welcome to projects
they not one sit programn for ~1h and be done
we can but then where is that value going
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.