#🔒 How can i condense this or is this as best as i am getting

122 messages · Page 1 of 1 (latest)

odd timber
#

aaa

atomic tartanBOT
#

@odd timber

Python help channel opened

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.

odd timber
#
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

outer ether
#

It might be a good idea to use classes

odd timber
#

is there a better way of doing it than this without them

outer ether
#

but if you don't want to learn abt them then that code works fine

odd timber
#

alright

#

i do want to learn about them

#

but i havent gotten onto it yet

#

im just trying to strengthen my current knowledge

outer ether
#

what would you like me to do?

odd timber
#

nothing

outer ether
odd timber
#

im listening

outer ether
#

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

odd timber
#

and in the list

#

it will be

#

2H 3H, 4S, 5S, 6C etc.

outer ether
#

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)
outer ether
odd timber
#

but then how do i

#

make 10-14

#

T,J,Q,K,A

outer ether
#

oh mb lemme modify that

outer ether
odd timber
#

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

brittle flume
#

You might want ... = int(value)

warped tartan
#

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.

odd timber
#

okay

brittle flume
#

It also looks like you have a space between the suits?

odd timber
#

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

west hawk
#

a dict for each suit isn't a great idea though. I would use a list for all the cards

odd timber
#

how can i assign the values

#

if im using a list though

#

create a tuple of value and card?

west hawk
#

exactly

odd timber
#

exactly to what i said?

west hawk
#

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)
atomic tartanBOT
#

@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', '♣')]
odd timber
#

alright ill try that thanks

west hawk
#

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

odd timber
#
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)
west hawk
#

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

odd timber
#

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

west hawk
#

pop is a method

#

deck.pop()

#

you have to call it

odd timber
#

oh right okay

#

thank you

west hawk
#

I would also only draw once for your player_draw function

#

then if you need to draw 2 cards, call it twice

odd timber
#

okay okay

#

thank you

west hawk
#

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))
odd timber
#

why is

#

returning the value

#

preferred

west hawk
#

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

odd timber
#

okay yea

#

i understand

west hawk
#

always try and write functions as generically as possible

#

it makes them more versatile and reusable

odd timber
#

okay

#

thank you

#

ill have a bash

#

and post in here i ncase of anything

west hawk
#

👍

odd timber
#
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

west hawk
#

use a parameter

odd timber
#

yea

#

just thought of that

odd timber
#
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

west hawk
#

I'd need the context of your full code

#

for a display function, printing from the function is fine

odd timber
#

ive sorted it haha

west hawk
#

no need to return there

odd timber
#

okay

odd timber
#

this is the code now

#

im currently struggling with getting the correct spacing

#

when printing out the cards

#

so that they allign

harsh heron
#

!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)

atomic tartanBOT
#

@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.
harsh heron
#

@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.

atomic tartanBOT
#
Python help channel closed

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.