Create Blackjack in Python | Beginner Friendly Tutorial

Ғылым және технология

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:
Patreon: / codecoach
Github: github.com/watsojar

Пікірлер: 88

  • @Delliott.
    @Delliott.2 жыл бұрын

    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.

  • @CodeCoachh

    @CodeCoachh

    2 жыл бұрын

    yes this should of been the logic i used to handle the aces I was not aware of this error in my code until I published the video!

  • @Ali-vw5he

    @Ali-vw5he

    Жыл бұрын

    is (hand) the player hand or something else?

  • @Delliott.

    @Delliott.

    Жыл бұрын

    @@Ali-vw5he (hand) is either the player hand or the dealers hand, you can pass either into the function.

  • @Ali-vw5he

    @Ali-vw5he

    Жыл бұрын

    @@Delliott. @Delliott 2 more questions, sorry. What is it meant by (turn) in the video for the first two functions, and also using your code from this comment i get the error " TypeError: unsupported operand type(s) for +: 'int' and 'str' "

  • @modelodesilhueta

    @modelodesilhueta

    Жыл бұрын

    I had the same problem thank you for solving it

  • @Jakebrah
    @Jakebrah Жыл бұрын

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

  • @Naruto-td7oo
    @Naruto-td7oo2 жыл бұрын

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

  • @CodeCoachh

    @CodeCoachh

    2 жыл бұрын

    thanks my man!

  • @user-ek1rf2zc9b

    @user-ek1rf2zc9b

    4 ай бұрын

    What are you, a fucking bot? This whole tutorial is fake, it's him laughing at people wasting their time

  • @samreither
    @samreither2 жыл бұрын

    great video again thanks my man!

  • @CodeCoachh

    @CodeCoachh

    2 жыл бұрын

    Thx mon

  • @nathansmith7976
    @nathansmith797610 ай бұрын

    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

  • @arnoldshavings7326
    @arnoldshavings7326 Жыл бұрын

    works, great tutorial

  • @kennnnel
    @kennnnel2 жыл бұрын

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

  • @CodeCoachh

    @CodeCoachh

    2 жыл бұрын

    Wow, thank you!

  • @frankstenner5377
    @frankstenner53776 ай бұрын

    well made, thank you !

  • @stealthycrafter9402
    @stealthycrafter9402 Жыл бұрын

    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?

  • @ricitos2001
    @ricitos20018 ай бұрын

    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

  • @_parsapr5341
    @_parsapr53412 жыл бұрын

    Thank you so much. it help me a lot.

  • @CodeCoachh

    @CodeCoachh

    2 жыл бұрын

    Np!

  • @masqueradinglampshade4369
    @masqueradinglampshade4369 Жыл бұрын

    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.

  • @CodeCoachh

    @CodeCoachh

    Жыл бұрын

    Making this using OOP would be a great way to test your skills!

  • @anairaphadke9333
    @anairaphadke93332 жыл бұрын

    Hey @CodeCoach could you pls list down the code? It's not in the link in the description. Thx so much(I'm subscribed btw)!

  • @hecticphusion
    @hecticphusion2 жыл бұрын

    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

  • @CodeCoachh

    @CodeCoachh

    2 жыл бұрын

    Yes I totally could but because this was a beginner tutorial I tried to use inbuilt commands that were more simple!

  • @faizo007
    @faizo0075 ай бұрын

    Well Done!

  • @eZaFJDUBB
    @eZaFJDUBB9 ай бұрын

    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

  • @pandaskrt1552
    @pandaskrt15522 жыл бұрын

    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 KZread vid and I just got some questions

  • @adamulla2822
    @adamulla2822 Жыл бұрын

    How could you go about making a simulation from these functions shown here?

  • @werewolf873
    @werewolf8732 жыл бұрын

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

  • @CodeCoachh

    @CodeCoachh

    2 жыл бұрын

    We have a separate check tie function

  • @bhaveshwadekar1553
    @bhaveshwadekar15532 жыл бұрын

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

  • @minholee6457
    @minholee6457 Жыл бұрын

    Hi, is there anyway to add a replay function to this?

  • @nathansmith7976

    @nathansmith7976

    10 ай бұрын

    Yes. Select all that code hit tab, and make a while loop. But late on the reply lol

  • @malekbaghdadi5862
    @malekbaghdadi5862 Жыл бұрын

    I tried on my own for a couple of hours , I feel very dumb after seeing you do it. My first project btw.

  • @CodeCoachh

    @CodeCoachh

    Жыл бұрын

    Trying on your own is the only way to get better. the more you do it the more intuitive it will start to seem for you!

  • @adamulla2822
    @adamulla2822 Жыл бұрын

    How could you do multiplayer using the functions here

  • @user-sk6xt6zt9p
    @user-sk6xt6zt9p Жыл бұрын

    I made one using classes You should try it

  • @gorkemhanoglu7483
    @gorkemhanoglu74832 жыл бұрын

    Awesome video bro, and can u share the source code also keep it go u will have much subscribers

  • @CodeCoachh

    @CodeCoachh

    2 жыл бұрын

    Check out the link to my github in the description!

  • @Shikori420

    @Shikori420

    2 жыл бұрын

    @@CodeCoachh this code is not in your github

  • @Heavy-J
    @Heavy-J Жыл бұрын

    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

  • @AK-uo1uw
    @AK-uo1uw Жыл бұрын

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

  • @R4zz3_
    @R4zz3_ Жыл бұрын

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

  • @lothedownlow4149
    @lothedownlow4149 Жыл бұрын

    The remove and append do not work for me

  • @vishalgoyal9180
    @vishalgoyal9180 Жыл бұрын

    i Can't find it in GITHUB can you check once

  • @mutasimbillah2925
    @mutasimbillah2925 Жыл бұрын

    How can I run this code for 1000 times and have a statistics of who won how many times?

  • @adamulla2822
    @adamulla2822 Жыл бұрын

    Is there a way you can make a simulation to run this like 100 times and get the results of each run? Excellent video btw

  • @adamulla2822

    @adamulla2822

    Жыл бұрын

    @@cianandrews9402 nope I haven't unfortunately :( I really need help with my computer science project and this is worth 30% of my grade

  • @matthewdefanti

    @matthewdefanti

    Жыл бұрын

    @@adamulla2822 Of course you can run it multiple times. You would just need to automate the process of hitting/standing which is where it gets a little more difficult.

  • @gedtoon6451
    @gedtoon64514 ай бұрын

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

  • @dawgsout4free
    @dawgsout4free Жыл бұрын

    how about draws/push?

  • @christhomasingebrigtsenjar4795

    @christhomasingebrigtsenjar4795

    Жыл бұрын

    you could write: elif total(playerHand) == total(dealerHand): print(f' You have {playerHand} for a total of {total(playerHand)} and the dealer has {dealerHand} for a total of {total(dealerHand)}') print(f" It's a push! No winners!")

  • @johnmigueldungao5051
    @johnmigueldungao5051 Жыл бұрын

    i am having trouble to understand this line of code in total method. elif card in face: total += 1 face card is value 10, i dont understand why its "1"

  • @bingito1

    @bingito1

    Жыл бұрын

    it's supposed to be 10, he later goes back and changes it

  • @GamingWithKJ_YT
    @GamingWithKJ_YT Жыл бұрын

    Would this be plagiarizing if I use part of this for my ap compsi principles project

  • @zzSparKzz3

    @zzSparKzz3

    2 ай бұрын

    Do u know if it is

  • @GamingWithKJ_YT

    @GamingWithKJ_YT

    2 ай бұрын

    @@zzSparKzz3 if you copy the code 100% yes. However I used ai for parts of mine and never got caught. Your better off writing code yourself than watching youtube.

  • @saliherenyuceturk2398
    @saliherenyuceturk23982 жыл бұрын

    eyyyyyy

  • @jtlearns2codecode
    @jtlearns2codecode2 жыл бұрын

    is there anyway you can post your completed code to git or somewhere? So i can make sure i have it right? I am having issues with it.

  • @CodeCoachh

    @CodeCoachh

    2 жыл бұрын

    The link to my github is in the description!

  • @jaynaii5181
    @jaynaii51812 жыл бұрын

    I started on my own for a bit then came back to see if I was on the right track, and I was COMPLETELY wrong (I did the deck wrong and my solution for the face card were just off). I really wanna know how to think like this. How to think like a programmer. I would have never come up with this if I had to to it by my self. Edit: like how did you think of the "21 - total(dealer_hand) > 21 - total(player_hand)" line. I still can't wrap my head around how you found that out.

  • @CodeCoachh

    @CodeCoachh

    2 жыл бұрын

    Hey Jay, a lot of the "thinking like a programmer" comes from experience. Knowing how similar applications are built helps you game plan how you re going to solve the problem and you do not learn this until you start building.

  • @Piktro
    @PiktroСағат бұрын

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

  • @kjelltingelstad7364
    @kjelltingelstad73642 жыл бұрын

    It is counting ace as 11 for me? I need help

  • @CodeCoachh

    @CodeCoachh

    2 жыл бұрын

    Check the pinned comment for a better way to handle aces i score Tru ally had an error in my code sorry for the confusion.

  • @VeNoM-xl4pc
    @VeNoM-xl4pc Жыл бұрын

    bro idk why it doesn't work for me guys pls help I'm really stuck when I run the module it doesn't show the amount of hand that is have

  • @CodeCoachh

    @CodeCoachh

    Жыл бұрын

    If you describe the issue more detailed then maybe I could help!

  • @berringervids
    @berringervids8 ай бұрын

    Totally noob at python, but why not use deck = [1,2,3....] and then do decks = deck*4?

  • @the_great_unknown1
    @the_great_unknown13 ай бұрын

    Can someone post the code plz need it for a project in my python class ain't tryna type allthat

  • @rommepomme13

    @rommepomme13

    Ай бұрын

    did u get it? pls post

  • @deblobvis264
    @deblobvis26411 ай бұрын

    Now i will use pygame for buttons and then make it an android app

  • @tadastybros4758
    @tadastybros4758 Жыл бұрын

    Can someone copy thé programme an past him here

  • @Jammzz_balisong
    @Jammzz_balisong7 ай бұрын

    the ace is 11 or 1

  • @Ali-vw5he
    @Ali-vw5he Жыл бұрын

    Please add this to your Github

  • @CodeCoachh

    @CodeCoachh

    Жыл бұрын

    I can send the code via discord (description) if you would like.

  • @Ali-vw5he

    @Ali-vw5he

    Жыл бұрын

    @@CodeCoachh No discord link in the description

  • @lightyamai9914
    @lightyamai99142 жыл бұрын

    I didn’t work for and I just wasted 3 hours bruh

  • @CodeCoachh

    @CodeCoachh

    2 жыл бұрын

    Can you describe what is not working I could help? Sorry you are having trouble.

  • @tdog7925
    @tdog7925 Жыл бұрын

    bad

  • @wageehelgammal5682

    @wageehelgammal5682

    Жыл бұрын

    is to possible to show us how to code blackjack using Classes(object oriented), Thanks

  • @Ali-vw5he
    @Ali-vw5he Жыл бұрын

    Hey could you please update your discord link in your account profile section, so I can contact you for help with this code?

Келесі