#🔒 How can i condense this or is this as best as i am getting
122 messages · Page 1 of 1 (latest)
@odd timber
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.
deck_of_diamonds = {}
deck_of_clubs = {}
deck_of_spades = {}
deck_of_hearts = {"AH": 10,
"KH": 10,
"QH": 10,
"JH": 10,
"TH": 10,
"9H": 9,
"8H": 8,
"7H": 7,
"6H": 6,
"5H": 5,
"4H": 4,
"3H": 3,
"2H": 2,}
for key, value in deck_of_hearts.items():
deck_of_spades[key.replace("H", "S")] = value
for key, value in deck_of_hearts.items():
deck_of_clubs[key.replace("H", "C")] = value
for key, value in deck_of_hearts.items():
deck_of_diamonds[key.replace("H", "D")] = value
deck = deck_of_diamonds| deck_of_hearts | deck_of_spades | deck_of_clubs
print(deck)
want to create a singular deck containing all suits for a game of blackjack
ive googled a little, but i dont have a knowledge of classes yet
It might be a good idea to use classes
is there a better way of doing it than this without them
but if you don't want to learn abt them then that code works fine
alright
i do want to learn about them
but i havent gotten onto it yet
im just trying to strengthen my current knowledge
what would you like me to do?
nothing
I think I know a way to optimize this without classes
im listening
you can make each card a dictionary like this probably
card = {
"suit": "H",
"value": 0
}
and then put a bunch of them in a list
well the total code is this
deck = []
for suit in "HCSD":
for value in ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']:
deck.append(value + suit)
i can do this too probably
oh mb lemme modify that
this probably does what you want
alright
i see that
thank you
for suit in "♠ ♥ ♦ ♣":
for value in ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]:
face_values = ["T", "J", "Q", "K", "A"]
if value in face_values:
deck[value + suit] = 10
else:
deck[value + suit] = value
this is what ive ended up with
You might want ... = int(value)
And this is minor, but I'd move face_values = out of the loops, or you'll be recreating that list on every iteration of the inner loop. It won't matter here, but it's worth keeping in mind.
okay
It also looks like you have a space between the suits?
yea let me
fix that
thank you
def player_draw():
player_cards.append(deck.pop(random))
player_cards.append(deck.pop(random))
player_draw()
print(player_cards)
this is my current issue
how does one
i want it to remove two random items from the dictionary
and add it to the player hand
ive considered putting the dictionary into a set
you can choose a key at random and remove it
a dict for each suit isn't a great idea though. I would use a list for all the cards
how can i assign the values
if im using a list though
create a tuple of value and card?
exactly
exactly to what i said?
yes, your deck would be a list of tuples
!e
deck = []
for rank in ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']:
for suit in "♠♥♦♣":
card = (rank, suit)
deck.append(card)
print(deck)
@west hawk :white_check_mark: Your 3.12 eval job has completed with return code 0.
[('A', '♠'), ('A', '♥'), ('A', '♦'), ('A', '♣'), ('2', '♠'), ('2', '♥'), ('2', '♦'), ('2', '♣'), ('3', '♠'), ('3', '♥'), ('3', '♦'), ('3', '♣'), ('4', '♠'), ('4', '♥'), ('4', '♦'), ('4', '♣'), ('5', '♠'), ('5', '♥'), ('5', '♦'), ('5', '♣'), ('6', '♠'), ('6', '♥'), ('6', '♦'), ('6', '♣'), ('7', '♠'), ('7', '♥'), ('7', '♦'), ('7', '♣'), ('8', '♠'), ('8', '♥'), ('8', '♦'), ('8', '♣'), ('9', '♠'), ('9', '♥'), ('9', '♦'), ('9', '♣'), ('10', '♠'), ('10', '♥'), ('10', '♦'), ('10', '♣'), ('J', '♠'), ('J', '♥'), ('J', '♦'), ('J', '♣'), ('Q', '♠'), ('Q', '♥'), ('Q', '♦'), ('Q', '♣'), ('K', '♠'), ('K', '♥'), ('K', '♦'), ('K', '♣')]
alright ill try that thanks
then you can easily shuffle the list or remove 2 random cards
shuffle the deck, then remove the cards from the end of the list to simulate "dealing" cards
for suit in "♠♥♦♣":
for rank in ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]:
if rank in face_values:
card = (rank + suit, 10)
deck.append(card)
else:
card = (rank + suit, rank)
deck.append(card)
don't worry about the value of the card just yet in how it pertains to a specific game
this is just a deck of playing cards. They have no intrinsic value. It's the game's responsibility to give value/meaning to each card
okay
[<built-in method pop of list object at 0x000002BD2102F700>, <built-in method pop of list object at 0x000002BD2102F700>]
def player_draw():
player_cards.append(deck.pop)
player_cards.append(deck.pop)
why lol
I would also only draw once for your player_draw function
then if you need to draw 2 cards, call it twice
in fact, you should return the dealt card
def deal_card(deck):
return deck.pop()
for _ in range(2):
player_cards.append(deal_card(deck))
more control
you get to decide where that card ends up
instead of a VERY specific function that only deals to the player
you now have a function that simply deals 1 card from the deck
always try and write functions as generically as possible
it makes them more versatile and reusable
👍
def player_card_value():
for card in player_cards:
if card[0] in face_values:
return 10
else:
return card[0]
def dealer_card_value():
for card in dealer_cards:
if card[0] in face_values:
return 10
else:
return card[0]
this is how i have gone about assigning values for each players cards
for card in player_cards:
player_value += player_card_value()
im not sure how to make it generic
as both hands are stored seperately
use a parameter
print(display_cards())
def display_cards():
return "\nYour cards: {} value: {} \nMy visible card: {} value: {}".format(player_cards, card_value(player_cards), dealer_cards, card_value(dealer_cards))
why is this not printing anything
even though it should print the new cards
I'd need the context of your full code
for a display function, printing from the function is fine
ive sorted it haha
no need to return there
okay
this is the code now
im currently struggling with getting the correct spacing
when printing out the cards
so that they allign
!e ```py
example_str = "Example"
s = f"""
This should be on a line by itself.
{example_str} is on the next line
This line starts eight spaces from the left of the screen.
"""
print(s)
@harsh heron :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 |
002 | This should be on a line by itself.
003 | Example is on the next line
004 | This line starts eight spaces from the left of the screen.
@odd timber
with f-string formatting you can also pad strings you insert
If you want more control over screen placement on the console you may consider a TUI library like curses or similar.
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.