Create Blackjack in Python | Beginner Friendly Tutorial

preview_player
Показать описание
Hey everyone, in today's video we create blackjack in python. This is a beginner friendly tutorial where I walk you through every line of code you need to create blackjack. Creating this program reinforces all the beginner programming concepts (variables, loops, functions, print, input, etc.) and is incredibly simple to code.

On this channel we focus on all things Python. Whether it is a project tutorial/showcase, teaching different Python concepts, or just live coding this is one of the best channels for programmers. Check out my links down below  👇

Links:
Рекомендации по теме
Комментарии
Автор

I think you need to re-work your 'total' function. Sometimes it'll falsely claim a player has gone bust even if they haven't, at least in my experience.

Try something along the following:

def determine_total(hand):
total = 0
ace_11s = 0
for card in hand:
if card in range(11):
total += card
elif card in ['J', 'K', 'Q']:
total += 10
else:
total += 11
ace_11s += 1
while ace_11s and total > 21:
total -= 10
ace_11s -= 1
return total

This worked 100% accurately for me.

Delliott.
Автор

How in the world do you only have 43 subscribers?? So professional, helped me a lot. Thanks

Naruto-tdoo
Автор

One quick thing you can do to simplify the dealCard function is to do turn.append(deck.pop(card)) since pop returns and removes the item at the given index

hecticphusion
Автор

you are one of the few coding teachers who appropriately explains each step, thank you! I am aware this can be done with classes to shorten the code, however seeing it done simply like this coming from another language is so beneficial

eZaFJDUBB
Автор

probably one of the most easy to understand tutorials Ive seen Thanks!

Jakebrah
Автор

I'm brand new to Python, haven't touched any code in 10+ years until 2 weeks ago, but wouldn't it be easier to check certain win conditions during each turn, or at least print the player and dealer hands at the end outside of the if statements, to avoid repeating all that code and all those print statements?

For example, immediately after dealing:
If player and dealer blackjack, print push.
Elif player blackjack, print player wins.
Elif dealer blackjack, print dealer wins.

In player turn:
If player busts, print dealer wins

In dealer turn:
If dealer busts, print player wins.

Then after a player "stand", just compare player vs dealer totals.
If player > dealer, print player wins.
Elif dealer > player, print dealer wins.

Even if we keep the code how it is, wouldn't something like the following be much easier to read?


print(f"Player has {total(player_hand)}. Dealer has {total(dealer_hand)}.")

if total(player_hand) == 21:
print(f"Blackjack, you win!")
elif total(dealer_hand) == 21:
print(f"Blackjack, dealer wins!")
elif total(player_hand) > 21:
print(f"Player bust! Dealer wins.")
elif total(dealer_hand) > 21:
print(f"Dealer bust! Player wins.")
elif 21 - total(dealer_hand) < 21 - total(player_hand):
print(f"You win!")
elif 21 - total(dealer_hand) > 21 - total(player_hand):
print(f"Dealer wins!")

I've just watched a bunch of tutorials and things like "don't repeat yourself" were hammered into my brain.

Piktro
Автор

why does my terminal say Dealer had<function revealdealerhand at and X but my hand is normal?

byuvhlu
Автор

Welp, thanks man no internet at the moment so all i have is python to take up my time. Just neeeded the code for a basic game and u got that to me, already added a gambling feature, replays, splits and doubling down. Just trying to add some more stuff before my internet gets back and i have video games again lol ty i was going stir crazy

nathansmith
Автор

your game logic does not include both player and dealer have 21 or player and dealer are both bust.

gedtoon
Автор

The remove and append do not work for me

lothedownlow
Автор

This video is very usefull because with this i ended my version of the game "BlackJack"
I gave yo my like and my sub
Thank you for help me

ricitos
Автор

I cannot use the random function/choice function.
TypeError: 'set' object is not subscriptable

AK-uouw
Автор

make sure you include an else statement at the end to take care of when your hands tie.

werewolf
Автор

I cant find the code on git. Someone can please help me get the given code...

bhaveshwadekar
Автор

My terminal is currently showing both of the dealers cards even tho it should show one and "X"

print(f"Dealer had {revealDealerHand()} and X")

Rzz_
Автор

hey let me get a response……….. I have an idea that i’m sure is possible but can’t get it to work your other videos of game automation with scripting inspired me

Heavy-J
Автор

This would be a great project to redo a second time using classes for the deck, dealer, and player and managing the hands totals using methods for each class.

Of course classes are probably the more advanced of entry level Python material, so totally see a use case for beginners trying this project with lists as well.

masqueradinglampshade
Автор

Is there a way to make it so that there are multiple playerhands? Would it be defining multiple playerhands and checking for there true or false values?

stealthycrafter
Автор

Hey if I could get your help that would be fantastic on like discord or something if you got it. You have so far been the most helpful out of every YouTube vid and I just got some questions

pandaskrt
Автор

I like your voice and speaking,
Professional and comfortable.
Thank you

kennnnel