Blackjack game - follow-up
$begingroup$
This is an updated version of my previously posted Blackjack game. I think I did almost everything @Austin Hastings recommend me to do and I hope you like it.
from random import shuffle
import os
def shuffled_shoe():
shoe = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'A', 'J', 'Q', 'K']*4
shuffle(shoe)
return shoe
def deal_card(shoe, person, number):
for _ in range(number):
person.append(shoe.pop())
def deal_hand(shoe, player, dealer):
deal_card(shoe, player, 2)
deal_card(shoe, dealer, 2)
def score(person):
non_aces = [c for c in person if c != 'A']
aces = [c for c in person if c == 'A']
total = 0
for card in non_aces:
if card in 'JQK':
total += 10
else:
total += int(card)
for card in aces:
if total <= 10:
total += 11
else:
total += 1
return total
def display_info(player, dealer, player_stands):
os.system('cls' if os.name == 'nt' else 'clear')
print("Your cards: [{}] ({})".format("][".join(player), score(player)))
if player_stands:
print("Dealer cards: [{}] ({})".format("][".join(dealer), score(dealer)))
else:
print(f"Dealer cards: [{dealer[0]}][?]")
def hit_or_stand():
while True:
print("What do you choose?")
print("[1] Hit")
print("[2] Stand")
ans = input("> ")
if ans in '12':
return ans
def player_play(shoe, player, dealer, player_plays, dealer_plays, player_stands):
while not player_stands:
if hit_or_stand() == '2':
player_plays = False
dealer_plays = True
player_stands = True
display_info(player, dealer, True)
elif not player_stands:
deal_card(shoe, player, 1)
display_info(player, dealer, False)
if score(player) >= 21:
player_plays = False
break
return (player_plays, dealer_plays, player_stands)
def dealer_play(shoe, dealer, DEALER_MINIMUM_SCORE, player):
while score(dealer) <= DEALER_MINIMUM_SCORE:
deal_card(shoe, dealer, 1)
display_info(player, dealer, True)
return False
def results(player, dealer, player_stands, still_playing):
if score(player) == 21:
print("Blackjack! You won")
still_playing = False
elif score(dealer) == 21:
print("Dealer got a blackjack. You lost!")
still_playing = False
elif score(player) > 21:
print("Busted! You lost!")
still_playing = False
if player_stands:
if score(dealer) > 21:
print("Dealer busted! You won")
elif score(player) > score(dealer):
print("You beat the dealer! You won!")
elif score(player) < score(dealer):
print("Dealer has beaten you. You lost!")
else:
print("Push. Nobody wins or losses.")
still_playing = False
return still_playing
def main():
shoe = shuffled_shoe()
player =
dealer =
player_plays = True
still_playing = True
dealer_plays = False
player_stands = False
deal_hand(shoe, player, dealer)
display_info(player, dealer, player_stands)
still_playing = results(player, dealer, player_stands, still_playing)
while still_playing:
while player_plays:
(player_plays, dealer_plays, player_stands) = player_play(shoe, player, dealer, player_plays, dealer_plays, player_stands)
while dealer_plays:
dealer_plays = dealer_play(shoe, dealer, 17, player)
still_playing = results(player, dealer, player_stands, still_playing)
if __name__ == '__main__':
main()
python python-3.x playing-cards
New contributor
$endgroup$
add a comment |
$begingroup$
This is an updated version of my previously posted Blackjack game. I think I did almost everything @Austin Hastings recommend me to do and I hope you like it.
from random import shuffle
import os
def shuffled_shoe():
shoe = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'A', 'J', 'Q', 'K']*4
shuffle(shoe)
return shoe
def deal_card(shoe, person, number):
for _ in range(number):
person.append(shoe.pop())
def deal_hand(shoe, player, dealer):
deal_card(shoe, player, 2)
deal_card(shoe, dealer, 2)
def score(person):
non_aces = [c for c in person if c != 'A']
aces = [c for c in person if c == 'A']
total = 0
for card in non_aces:
if card in 'JQK':
total += 10
else:
total += int(card)
for card in aces:
if total <= 10:
total += 11
else:
total += 1
return total
def display_info(player, dealer, player_stands):
os.system('cls' if os.name == 'nt' else 'clear')
print("Your cards: [{}] ({})".format("][".join(player), score(player)))
if player_stands:
print("Dealer cards: [{}] ({})".format("][".join(dealer), score(dealer)))
else:
print(f"Dealer cards: [{dealer[0]}][?]")
def hit_or_stand():
while True:
print("What do you choose?")
print("[1] Hit")
print("[2] Stand")
ans = input("> ")
if ans in '12':
return ans
def player_play(shoe, player, dealer, player_plays, dealer_plays, player_stands):
while not player_stands:
if hit_or_stand() == '2':
player_plays = False
dealer_plays = True
player_stands = True
display_info(player, dealer, True)
elif not player_stands:
deal_card(shoe, player, 1)
display_info(player, dealer, False)
if score(player) >= 21:
player_plays = False
break
return (player_plays, dealer_plays, player_stands)
def dealer_play(shoe, dealer, DEALER_MINIMUM_SCORE, player):
while score(dealer) <= DEALER_MINIMUM_SCORE:
deal_card(shoe, dealer, 1)
display_info(player, dealer, True)
return False
def results(player, dealer, player_stands, still_playing):
if score(player) == 21:
print("Blackjack! You won")
still_playing = False
elif score(dealer) == 21:
print("Dealer got a blackjack. You lost!")
still_playing = False
elif score(player) > 21:
print("Busted! You lost!")
still_playing = False
if player_stands:
if score(dealer) > 21:
print("Dealer busted! You won")
elif score(player) > score(dealer):
print("You beat the dealer! You won!")
elif score(player) < score(dealer):
print("Dealer has beaten you. You lost!")
else:
print("Push. Nobody wins or losses.")
still_playing = False
return still_playing
def main():
shoe = shuffled_shoe()
player =
dealer =
player_plays = True
still_playing = True
dealer_plays = False
player_stands = False
deal_hand(shoe, player, dealer)
display_info(player, dealer, player_stands)
still_playing = results(player, dealer, player_stands, still_playing)
while still_playing:
while player_plays:
(player_plays, dealer_plays, player_stands) = player_play(shoe, player, dealer, player_plays, dealer_plays, player_stands)
while dealer_plays:
dealer_plays = dealer_play(shoe, dealer, 17, player)
still_playing = results(player, dealer, player_stands, still_playing)
if __name__ == '__main__':
main()
python python-3.x playing-cards
New contributor
$endgroup$
add a comment |
$begingroup$
This is an updated version of my previously posted Blackjack game. I think I did almost everything @Austin Hastings recommend me to do and I hope you like it.
from random import shuffle
import os
def shuffled_shoe():
shoe = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'A', 'J', 'Q', 'K']*4
shuffle(shoe)
return shoe
def deal_card(shoe, person, number):
for _ in range(number):
person.append(shoe.pop())
def deal_hand(shoe, player, dealer):
deal_card(shoe, player, 2)
deal_card(shoe, dealer, 2)
def score(person):
non_aces = [c for c in person if c != 'A']
aces = [c for c in person if c == 'A']
total = 0
for card in non_aces:
if card in 'JQK':
total += 10
else:
total += int(card)
for card in aces:
if total <= 10:
total += 11
else:
total += 1
return total
def display_info(player, dealer, player_stands):
os.system('cls' if os.name == 'nt' else 'clear')
print("Your cards: [{}] ({})".format("][".join(player), score(player)))
if player_stands:
print("Dealer cards: [{}] ({})".format("][".join(dealer), score(dealer)))
else:
print(f"Dealer cards: [{dealer[0]}][?]")
def hit_or_stand():
while True:
print("What do you choose?")
print("[1] Hit")
print("[2] Stand")
ans = input("> ")
if ans in '12':
return ans
def player_play(shoe, player, dealer, player_plays, dealer_plays, player_stands):
while not player_stands:
if hit_or_stand() == '2':
player_plays = False
dealer_plays = True
player_stands = True
display_info(player, dealer, True)
elif not player_stands:
deal_card(shoe, player, 1)
display_info(player, dealer, False)
if score(player) >= 21:
player_plays = False
break
return (player_plays, dealer_plays, player_stands)
def dealer_play(shoe, dealer, DEALER_MINIMUM_SCORE, player):
while score(dealer) <= DEALER_MINIMUM_SCORE:
deal_card(shoe, dealer, 1)
display_info(player, dealer, True)
return False
def results(player, dealer, player_stands, still_playing):
if score(player) == 21:
print("Blackjack! You won")
still_playing = False
elif score(dealer) == 21:
print("Dealer got a blackjack. You lost!")
still_playing = False
elif score(player) > 21:
print("Busted! You lost!")
still_playing = False
if player_stands:
if score(dealer) > 21:
print("Dealer busted! You won")
elif score(player) > score(dealer):
print("You beat the dealer! You won!")
elif score(player) < score(dealer):
print("Dealer has beaten you. You lost!")
else:
print("Push. Nobody wins or losses.")
still_playing = False
return still_playing
def main():
shoe = shuffled_shoe()
player =
dealer =
player_plays = True
still_playing = True
dealer_plays = False
player_stands = False
deal_hand(shoe, player, dealer)
display_info(player, dealer, player_stands)
still_playing = results(player, dealer, player_stands, still_playing)
while still_playing:
while player_plays:
(player_plays, dealer_plays, player_stands) = player_play(shoe, player, dealer, player_plays, dealer_plays, player_stands)
while dealer_plays:
dealer_plays = dealer_play(shoe, dealer, 17, player)
still_playing = results(player, dealer, player_stands, still_playing)
if __name__ == '__main__':
main()
python python-3.x playing-cards
New contributor
$endgroup$
This is an updated version of my previously posted Blackjack game. I think I did almost everything @Austin Hastings recommend me to do and I hope you like it.
from random import shuffle
import os
def shuffled_shoe():
shoe = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'A', 'J', 'Q', 'K']*4
shuffle(shoe)
return shoe
def deal_card(shoe, person, number):
for _ in range(number):
person.append(shoe.pop())
def deal_hand(shoe, player, dealer):
deal_card(shoe, player, 2)
deal_card(shoe, dealer, 2)
def score(person):
non_aces = [c for c in person if c != 'A']
aces = [c for c in person if c == 'A']
total = 0
for card in non_aces:
if card in 'JQK':
total += 10
else:
total += int(card)
for card in aces:
if total <= 10:
total += 11
else:
total += 1
return total
def display_info(player, dealer, player_stands):
os.system('cls' if os.name == 'nt' else 'clear')
print("Your cards: [{}] ({})".format("][".join(player), score(player)))
if player_stands:
print("Dealer cards: [{}] ({})".format("][".join(dealer), score(dealer)))
else:
print(f"Dealer cards: [{dealer[0]}][?]")
def hit_or_stand():
while True:
print("What do you choose?")
print("[1] Hit")
print("[2] Stand")
ans = input("> ")
if ans in '12':
return ans
def player_play(shoe, player, dealer, player_plays, dealer_plays, player_stands):
while not player_stands:
if hit_or_stand() == '2':
player_plays = False
dealer_plays = True
player_stands = True
display_info(player, dealer, True)
elif not player_stands:
deal_card(shoe, player, 1)
display_info(player, dealer, False)
if score(player) >= 21:
player_plays = False
break
return (player_plays, dealer_plays, player_stands)
def dealer_play(shoe, dealer, DEALER_MINIMUM_SCORE, player):
while score(dealer) <= DEALER_MINIMUM_SCORE:
deal_card(shoe, dealer, 1)
display_info(player, dealer, True)
return False
def results(player, dealer, player_stands, still_playing):
if score(player) == 21:
print("Blackjack! You won")
still_playing = False
elif score(dealer) == 21:
print("Dealer got a blackjack. You lost!")
still_playing = False
elif score(player) > 21:
print("Busted! You lost!")
still_playing = False
if player_stands:
if score(dealer) > 21:
print("Dealer busted! You won")
elif score(player) > score(dealer):
print("You beat the dealer! You won!")
elif score(player) < score(dealer):
print("Dealer has beaten you. You lost!")
else:
print("Push. Nobody wins or losses.")
still_playing = False
return still_playing
def main():
shoe = shuffled_shoe()
player =
dealer =
player_plays = True
still_playing = True
dealer_plays = False
player_stands = False
deal_hand(shoe, player, dealer)
display_info(player, dealer, player_stands)
still_playing = results(player, dealer, player_stands, still_playing)
while still_playing:
while player_plays:
(player_plays, dealer_plays, player_stands) = player_play(shoe, player, dealer, player_plays, dealer_plays, player_stands)
while dealer_plays:
dealer_plays = dealer_play(shoe, dealer, 17, player)
still_playing = results(player, dealer, player_stands, still_playing)
if __name__ == '__main__':
main()
python python-3.x playing-cards
python python-3.x playing-cards
New contributor
New contributor
edited 15 mins ago
Jamal♦
30.3k11120227
30.3k11120227
New contributor
asked 16 hours ago
Maria LauraMaria Laura
383
383
New contributor
New contributor
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Maria Laura is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214921%2fblackjack-game-follow-up%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Maria Laura is a new contributor. Be nice, and check out our Code of Conduct.
Maria Laura is a new contributor. Be nice, and check out our Code of Conduct.
Maria Laura is a new contributor. Be nice, and check out our Code of Conduct.
Maria Laura is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214921%2fblackjack-game-follow-up%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown