Create Blackjack in Python | Beginner Friendly Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ธ.ค. 2024

ความคิดเห็น • 91

  • @Delliott.
    @Delliott. 2 ปีที่แล้ว +31

    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 ปีที่แล้ว +15

      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 2 ปีที่แล้ว +1

      is (hand) the player hand or something else?

    • @Delliott.
      @Delliott. 2 ปีที่แล้ว +1

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

    • @Ali-vw5he
      @Ali-vw5he 2 ปีที่แล้ว +2

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

  • @Naruto-td7oo
    @Naruto-td7oo 3 ปีที่แล้ว +17

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

    • @CodeCoachh
      @CodeCoachh  3 ปีที่แล้ว +1

      thanks my man!

    • @JamesSmith-c9f
      @JamesSmith-c9f 10 หลายเดือนก่อน

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

  • @huehuehue-x3c
    @huehuehue-x3c 3 ปีที่แล้ว +8

    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  3 ปีที่แล้ว +5

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

  • @Jakebrah
    @Jakebrah 2 ปีที่แล้ว +5

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

  • @Piktro
    @Piktro 5 หลายเดือนก่อน +1

    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.

  • @eZaFJDUBB
    @eZaFJDUBB ปีที่แล้ว

    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

  • @nathansmith7976
    @nathansmith7976 ปีที่แล้ว +1

    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

  • @werewolf873
    @werewolf873 2 ปีที่แล้ว +1

    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

  • @sethbanakos4838
    @sethbanakos4838 3 หลายเดือนก่อน

    I need help on line 46 its giving me a syntax error for the X what do I do

  • @oogabooga-j8r
    @oogabooga-j8r 5 หลายเดือนก่อน +1

    why does my terminal say Dealer had and X but my hand is normal?

    • @oogabooga-j8r
      @oogabooga-j8r 5 หลายเดือนก่อน

      this is the code that i use :
      def revealdealerhand():
      if len(dealerhand) == 2:
      return dealerhand [0]
      elif len(dealerhand) > 2:
      return dealerhand [0], dealerhand[1]

  • @stealthycrafter9402
    @stealthycrafter9402 2 ปีที่แล้ว +1

    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?

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

  • @AK-uo1uw
    @AK-uo1uw ปีที่แล้ว

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

  • @minholee6457
    @minholee6457 2 ปีที่แล้ว +2

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

    • @nathansmith7976
      @nathansmith7976 ปีที่แล้ว

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

  • @ricitos2001
    @ricitos2001 ปีที่แล้ว

    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

  • @kennnnel
    @kennnnel 2 ปีที่แล้ว

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

  • @adamulla2822
    @adamulla2822 ปีที่แล้ว

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

  • @lothedownlow4149
    @lothedownlow4149 2 ปีที่แล้ว +1

    The remove and append do not work for me

  • @_parsapr5341
    @_parsapr5341 3 ปีที่แล้ว +1

    Thank you so much. it help me a lot.

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

  • @masqueradinglampshade4369
    @masqueradinglampshade4369 2 ปีที่แล้ว

    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  2 ปีที่แล้ว

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

  • @jtlearns2codecode
    @jtlearns2codecode 3 ปีที่แล้ว +4

    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  3 ปีที่แล้ว +1

      The link to my github is in the description!

  • @bhaveshwadekar1553
    @bhaveshwadekar1553 3 ปีที่แล้ว +1

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

  • @adamulla2822
    @adamulla2822 ปีที่แล้ว

    How could you do multiplayer using the functions here

  • @jaynaii5181
    @jaynaii5181 2 ปีที่แล้ว +3

    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 ปีที่แล้ว +3

      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.

  • @vishalgoyal9180
    @vishalgoyal9180 ปีที่แล้ว

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

  • @anairaphadke9333
    @anairaphadke9333 2 ปีที่แล้ว +1

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

  • @МИЛОШ-ч8и
    @МИЛОШ-ч8и 2 ปีที่แล้ว +1

    I made one using classes
    You should try it

  • @arnoldshavings7326
    @arnoldshavings7326 ปีที่แล้ว

    works, great tutorial

  • @berringervids
    @berringervids ปีที่แล้ว

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

  • @frankstenner5377
    @frankstenner5377 11 หลายเดือนก่อน

    well made, thank you !

  • @gedtoon6451
    @gedtoon6451 10 หลายเดือนก่อน +1

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

  • @johnmigueldungao5051
    @johnmigueldungao5051 2 ปีที่แล้ว

    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

  • @mutasimbillah2925
    @mutasimbillah2925 2 ปีที่แล้ว

    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.

  • @a7aim
    @a7aim 11 หลายเดือนก่อน

    Well Done!

  • @VeNoM-xl4pc
    @VeNoM-xl4pc 2 ปีที่แล้ว

    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  2 ปีที่แล้ว

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

  • @GamingWithKJ_YT
    @GamingWithKJ_YT ปีที่แล้ว

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

    • @zzSparKzz3
      @zzSparKzz3 8 หลายเดือนก่อน

      Do u know if it is

    • @GamingWithKJ_YT
      @GamingWithKJ_YT 8 หลายเดือนก่อน

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

  • @malekbaghdadi5862
    @malekbaghdadi5862 2 ปีที่แล้ว

    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  2 ปีที่แล้ว +1

      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!

  • @the_great_unknown1
    @the_great_unknown1 9 หลายเดือนก่อน

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

    • @rommepomme13
      @rommepomme13 7 หลายเดือนก่อน

      did u get it? pls post

  • @Pav711
    @Pav711 4 หลายเดือนก่อน

    Thanks!

  • @gorkemhanoglu7483
    @gorkemhanoglu7483 3 ปีที่แล้ว +3

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

    • @CodeCoachh
      @CodeCoachh  3 ปีที่แล้ว

      Check out the link to my github in the description!

    • @Shikori420
      @Shikori420 3 ปีที่แล้ว +7

      @@CodeCoachh this code is not in your github

  • @kjelltingelstad7364
    @kjelltingelstad7364 2 ปีที่แล้ว

    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.

  • @pandaskrt1552
    @pandaskrt1552 3 ปีที่แล้ว +2

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

  • @dawgsout4free
    @dawgsout4free 2 ปีที่แล้ว

    how about draws/push?

    • @christhomasingebrigtsenjar4795
      @christhomasingebrigtsenjar4795 2 ปีที่แล้ว

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

  • @deblobvis264
    @deblobvis264 ปีที่แล้ว

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

  • @tadastybros4758
    @tadastybros4758 2 ปีที่แล้ว

    Can someone copy thé programme an past him here

  • @Jammzz_balisong
    @Jammzz_balisong ปีที่แล้ว

    the ace is 11 or 1

  • @saliherenyuceturk2398
    @saliherenyuceturk2398 3 ปีที่แล้ว +3

    eyyyyyy

  • @Ali-vw5he
    @Ali-vw5he 2 ปีที่แล้ว

    Please add this to your Github

    • @CodeCoachh
      @CodeCoachh  2 ปีที่แล้ว

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

    • @Ali-vw5he
      @Ali-vw5he 2 ปีที่แล้ว

      @@CodeCoachh No discord link in the description

  • @lightyamai9914
    @lightyamai9914 2 ปีที่แล้ว

    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 2 ปีที่แล้ว +1

    bad

    • @wageehelgammal5682
      @wageehelgammal5682 2 ปีที่แล้ว

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

  • @Crismolti
    @Crismolti หลายเดือนก่อน

    I am sorry, maybe i dont understand something, but i cant fine double in your cod. For example we have 8 and 2.In a real game a lot of playerы will double and not just hit. I am really bad in a python and that is why i can do it myself. I am not from english speakers countries. Sorry for my English))

  • @Ali-vw5he
    @Ali-vw5he 2 ปีที่แล้ว

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