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
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
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
Жыл бұрын
is (hand) the player hand or something else?
@Delliott.
Жыл бұрын
@@Ali-vw5he (hand) is either the player hand or the dealers hand, you can pass either into the function.
@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
Жыл бұрын
I had the same problem thank you for solving it
probably one of the most easy to understand tutorials Ive seen Thanks!
How in the world do you only have 43 subscribers?? So professional, helped me a lot. Thanks
@CodeCoachh
2 жыл бұрын
thanks my man!
@user-ek1rf2zc9b
4 ай бұрын
What are you, a fucking bot? This whole tutorial is fake, it's him laughing at people wasting their time
great video again thanks my man!
@CodeCoachh
2 жыл бұрын
Thx mon
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
works, great tutorial
I like your voice and speaking, Professional and comfortable. Thank you
@CodeCoachh
2 жыл бұрын
Wow, thank you!
well made, thank you !
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?
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
Thank you so much. it help me a lot.
@CodeCoachh
2 жыл бұрын
Np!
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
Жыл бұрын
Making this using OOP would be a great way to test your skills!
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)!
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
2 жыл бұрын
Yes I totally could but because this was a beginner tutorial I tried to use inbuilt commands that were more simple!
Well Done!
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
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
How could you go about making a simulation from these functions shown here?
make sure you include an else statement at the end to take care of when your hands tie.
@CodeCoachh
2 жыл бұрын
We have a separate check tie function
I cant find the code on git. Someone can please help me get the given code...
Hi, is there anyway to add a replay function to this?
@nathansmith7976
10 ай бұрын
Yes. Select all that code hit tab, and make a while loop. But late on the reply lol
I tried on my own for a couple of hours , I feel very dumb after seeing you do it. My first project btw.
@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!
How could you do multiplayer using the functions here
I made one using classes You should try it
Awesome video bro, and can u share the source code also keep it go u will have much subscribers
@CodeCoachh
2 жыл бұрын
Check out the link to my github in the description!
@Shikori420
2 жыл бұрын
@@CodeCoachh this code is not in your github
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
I cannot use the random function/choice function. TypeError: 'set' object is not subscriptable
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")
The remove and append do not work for me
i Can't find it in GITHUB can you check once
How can I run this code for 1000 times and have a statistics of who won how many times?
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
Жыл бұрын
@@cianandrews9402 nope I haven't unfortunately :( I really need help with my computer science project and this is worth 30% of my grade
@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.
your game logic does not include both player and dealer have 21 or player and dealer are both bust.
how about draws/push?
@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!")
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
Жыл бұрын
it's supposed to be 10, he later goes back and changes it
Would this be plagiarizing if I use part of this for my ap compsi principles project
@zzSparKzz3
2 ай бұрын
Do u know if it is
@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.
eyyyyyy
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
2 жыл бұрын
The link to my github is in the description!
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
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.
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.
It is counting ace as 11 for me? I need help
@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.
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
Жыл бұрын
If you describe the issue more detailed then maybe I could help!
Totally noob at python, but why not use deck = [1,2,3....] and then do decks = deck*4?
Can someone post the code plz need it for a project in my python class ain't tryna type allthat
@rommepomme13
Ай бұрын
did u get it? pls post
Now i will use pygame for buttons and then make it an android app
Can someone copy thé programme an past him here
the ace is 11 or 1
Please add this to your Github
@CodeCoachh
Жыл бұрын
I can send the code via discord (description) if you would like.
@Ali-vw5he
Жыл бұрын
@@CodeCoachh No discord link in the description
I didn’t work for and I just wasted 3 hours bruh
@CodeCoachh
2 жыл бұрын
Can you describe what is not working I could help? Sorry you are having trouble.
bad
@wageehelgammal5682
Жыл бұрын
is to possible to show us how to code blackjack using Classes(object oriented), Thanks
Hey could you please update your discord link in your account profile section, so I can contact you for help with this code?