#๐ Black Jack help pt.3 (ft. mental harrassment)
57 messages ยท Page 1 of 1 (latest)
@violet gate
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.
import random
cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
player_deck = []
dealer_deck = []
def deal_cards(player, dealer):
'''This function deals individual cards to each deck'''
total_player = player.append(random.choice(cards))
total_dealer = dealer.append(random.choice(cards))
def calculate_score(deck):
'''This function calculates scores, if the sum of a deck is above 21 it returns 0, else it returns its sum'''
if sum(deck) == 21:
return 0
else:
p_score = sum(deck)
return p_score
def compare(p_score,d_score):
'''this function compares the decks, if its zero the deck wins then either they lose or add new cards'''
if p_score == d_score:
print("its a draw")
return False
elif p_score == 0:
print("You win")
return False
elif d_score == 0:
print("You lose")
return False
elif p_score > 21:
print("you went over, Dealer wins!")
return False
elif d_score > 21:
print("Dealer went over, You win")
return False
else:
return True
def main():
player_score = 0
dealer_score = 0
f_input = input("Would you like to play a game of blackjack (y/n): ").lower()
if f_input == "y":
for i in range(2):
deal_cards(player_deck, dealer_deck)
game_running = compare(player_score,dealer_score)
while game_running:
print(f"your cards are {player_deck}, Your current score is {calculate_score(player_deck)}")
print(f"Dealers first hand is {dealer_deck[0]}")
new_card = input("Would you like to add cards(y/n): ").lower()
if new_card == "y":
deal_cards(player_deck,dealer_deck)
player_score = calculate_score(player_deck)
dealer_score = calculate_score(dealer_deck)
compare(player_score,dealer_score)
else:
pass
main()
The only thing i cant seem to get my head around is the while loops, idk how and when i ask the player if they need new cards. and then to determine the winner. then the 17 problem. i.e, if the player doesnt require new cards, and the dealer cards are less than 17, the dealee needs to deal cards so the score gets above or equal to 17
I'll make some observations as I go. They may or may not have bearing on the specific thing you've asked about.
alright
Something about how and when you're doing the compare call is catching my attention.
Something smells off about it, possibly. I can't articulate it just yet.
In the game of Blackjack or something of the sort, you're adding cards to your hand until you either go bust or you elect to...I think it's called stand?
i suppose, yeah
There should probably be checking for sums over 21 during this decision loop.
Now, there is a check for that in the loop, however...
over? in calculate function i return 0 if any of the values i.e player_score or dealer_score is 21. So its easier to understand who wins
which later gets carried out to the compare function
If at any point in this decision loop, the dealer and the player have equal hands, this results in a draw.
This may be incorrect.
At least partially.
Would you like to play a game of blackjack (y/n): y
its a draw```
Hm.
yeah i got it
but if the player gets 21 as his first two cards, he automatically wins with no further addition of cards
how do i figure that
If you give me a few minutes, I'll see if I can whip up my own implementation of the game roughly along the lines of what you're attempting, so I have a clearer idea in my own head of the required logic and flow.
Okay, so while I haven't made my way through all of the logic, the precedence of which makes my brain go a bit wibbly...I don't think writing functions like this is really called for.
Unless you're supposed to write functions as part of a requirement.
I think in this case they may be an unnecessary abstraction that just adds complexity.
Which goes against my usual grain of saying "ABSTRACT ALL THE LOGIC!"
Damn
Im thinking of going through the solution, writing down what blunders i made and do the entire thing from scratch
So i learn
How does that sound
I will often scrap an entire project and rewrite it.
Some people say "NONONO, don't do that!"
I find that through rewriting a project over and over, one gains a greater awareness of the essential components of the logic.
Burn away all the rest, and you're left with all you need.
Jettison the superfluous!
Yeah its my second time doing this from scratch
God are you wise
Thanks man
Am I? I thought I was just up myself.
Now i understand how confusing my work was
Sometimes i act similar, no one notices and i never try that again
I think all you might need, essentially, is a while loop, some checking logic in that, and a little bit of logic checking outside of the while loop.
You might have another while loop to force correct user input.
A for loop could be used to hit two cards, I suppose. Isn't that another player choice? To hit one or two? I can't remember.
I suppose there would be variations.
To deal cards i already have a for loop
You may even take advantage of break and while's else.
one other thing I am seeing is that it looks like on a single prompt to the player asking for a card you seem to be dealing a card to both the dealer and the player. The stand/hit of each player is not dependent on the other's choices. Yes there is the 17 problem as you noted but you might benefit from changing your deal_cards to simply deal a card to the 'player' that requested it, instead of both each time.
There are other more advanced blackjack rules like splitting however but that can come after the core is running well.
But it is important that the dealer's hits and player's hits aren't connected together. (other than alternating in choice)
missing: A is always counted as 11. in blackjack, it could be 1
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.