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