patternpythonMinor
Simple Blackjack game in Python 3.4
Viewed 0 times
gamesimplepythonblackjack
Problem
I made a simple Blackjack game in Python 3.4.3. I'm a beginner, so it would be awesome if you could rip up my code and tell me everything I did wrong. I know there's no insurance for when the dealer has an ace (yet), and there's some other blackjack rules that I'm going to add in the future, but I wanted to get a base code out there.
```
import random as r
import itertools as i
suit = 'scdh'
rank = '23456789TJQKA'
deck = tuple(''.join(card) for card in i.product(rank, suit))
val = ()
for _ in range(9):
val = val + (_+2, _+2, _+2, _+2)
if _ == 8:
for __ in range(3):
val = val + (10, 10, 10, 10)
val = val + (1, 1, 1, 1)
deckval = dict(zip(deck, val))
def deal():
global hand, dealer_hand, player_hand, counter
hand = r.sample(deck, 52)
counter = 0
dealer_hand = list(hand[counter:counter + 1])
counter += 2
player_hand = list(hand[counter:counter + 2])
counter += 2
def sum_player_hand():
global hand, player_hand, counter, player_sum, opt_player_sum
player_sum = 0
opt_player_sum = 0
for a in range(len(player_hand)):
if int(deckval[player_hand[a]]) == 1 and opt_player_sum + int(deckval[player_hand[a]]) 21:
player_sum += int(deckval[player_hand[a]])
opt_player_sum = player_sum
else:
player_sum += int(deckval[player_hand[a]])
opt_player_sum += int(deckval[player_hand[a]])
def dealer_init():
global hand, dealer_hand, counter, dealer_sum, opt_dealer_sum
dealer_sum = 0
opt_dealer_sum = 0
if int(deckval[dealer_hand[0]]) == 1:
dealer_sum += int(deckval[dealer_hand[0]])
opt_dealer_sum += dealer_sum + 10
else:
dealer_sum = int(deckval[dealer_hand[0]])
opt_dealer_sum = int(deckval[dealer_hand[0]])
dealer_logic()
def dealer_logic():
global hand, dealer_hand, counter, dealer_sum, opt_dealer_sum
if dealer_sum >= 17 or opt_dealer_sum >= 17:
pass
else:
while opt_dea
```
import random as r
import itertools as i
suit = 'scdh'
rank = '23456789TJQKA'
deck = tuple(''.join(card) for card in i.product(rank, suit))
val = ()
for _ in range(9):
val = val + (_+2, _+2, _+2, _+2)
if _ == 8:
for __ in range(3):
val = val + (10, 10, 10, 10)
val = val + (1, 1, 1, 1)
deckval = dict(zip(deck, val))
def deal():
global hand, dealer_hand, player_hand, counter
hand = r.sample(deck, 52)
counter = 0
dealer_hand = list(hand[counter:counter + 1])
counter += 2
player_hand = list(hand[counter:counter + 2])
counter += 2
def sum_player_hand():
global hand, player_hand, counter, player_sum, opt_player_sum
player_sum = 0
opt_player_sum = 0
for a in range(len(player_hand)):
if int(deckval[player_hand[a]]) == 1 and opt_player_sum + int(deckval[player_hand[a]]) 21:
player_sum += int(deckval[player_hand[a]])
opt_player_sum = player_sum
else:
player_sum += int(deckval[player_hand[a]])
opt_player_sum += int(deckval[player_hand[a]])
def dealer_init():
global hand, dealer_hand, counter, dealer_sum, opt_dealer_sum
dealer_sum = 0
opt_dealer_sum = 0
if int(deckval[dealer_hand[0]]) == 1:
dealer_sum += int(deckval[dealer_hand[0]])
opt_dealer_sum += dealer_sum + 10
else:
dealer_sum = int(deckval[dealer_hand[0]])
opt_dealer_sum = int(deckval[dealer_hand[0]])
dealer_logic()
def dealer_logic():
global hand, dealer_hand, counter, dealer_sum, opt_dealer_sum
if dealer_sum >= 17 or opt_dealer_sum >= 17:
pass
else:
while opt_dea
Solution
There are just a few nitpicky things that I want to cover.
That's about all I can come up with. If there's anything else that you want me to cover, just mention it in the comments, and I'll see what I can do. Hope this helps!
- Don't use
_in a for loop unless you don't use the value. The variable_is slightly unclear anyways.
- There is no need to make module names one letter, like
itertoolsasi, orrandomasr. It makes the code much harder to read.
- Add some docstrings to your functions to describe what they do. While well written code should be fairly easy to read, you should still have comments that describe what the function does, and how it works.
- Finally, at the end of the file where you run
deal(),main(), andrun(), should be underneath anif __name__ == "__main__":.
That's about all I can come up with. If there's anything else that you want me to cover, just mention it in the comments, and I'll see what I can do. Hope this helps!
Context
StackExchange Code Review Q#93938, answer score: 5
Revisions (0)
No revisions yet.