I’ve solely been studying Python for just a few days after a Humble Bundle ebook sale, however I made a purposeful Blackjack sport on which I would like some constructive criticism so far as my coding construction and any ideas for enchancment from extra skilled coders.
Edit: That is coded in Python 3.8.2
import random
import time
deck = []
firstDraw = 0
usedCards = []
playerHand = []
playerHandValue = 0
dealtCard1 = ''
dealtCard1Number = 0
dealtCard2 = ''
dealtCard2Number = 0
dealerHand = []
dealerHandValue = 0
def dealHand():
world deck
world playerHand
world playerHandValue
world dealerHand
world dealerHandValue
world firstDraw
firstDraw = 0
# The beginning deck is created #
deck = ['2sp', '3sp', '4sp', '5sp', '6sp', '7sp', '8sp', '9sp', '10sp', 'Jsp', 'Qsp', 'Ksp', 'Asp', '2cl', '3cl', '4cl', '5cl', '6cl', '7cl', '8cl', '9cl', '10cl', 'Jcl', 'Qcl', 'Kcl', 'Acl', '2he', '3he', '4he', '5he', '6he', '7he', '8he', '9he', '10he', 'Jhe', 'Qhe', 'Khe', 'Ahe', '2di', '3di', '4di', '5di', '6di', '7di', '8di', '9di', '10di', 'Jdi', 'Qdi', 'Kdi', 'Asp']
playerHand = []
playerHandValue = 0
dealerHand = []
dealerHandValue = 0
# Two playing cards are dealt to the participant #
dealtCard1Number = random.randint(0, len(deck)-1)
playerHand.append(deck[dealtCard1Number])
del deck[dealtCard1Number]
dealtCard2Number = random.randint(0, len(deck)-1)
playerHand.append(deck[dealtCard2Number])
del deck[dealtCard2Number]
# Two playing cards are dealt to the seller #
dealerCard1Number = random.randint(0, len(deck)-1)
dealerCard1 = deck[dealerCard1Number]
dealerHand.append(dealerCard1)
del deck[dealerCard1Number]
dealerCard2Number = random.randint(0, len(deck)-1)
dealerCard2 = deck[dealerCard2Number]
dealerHand.append(dealerCard2)
del deck[dealerCard2Number]
# The participant's beginning hand is revealed to the participant #
print('n' + 'Your present hand is ' + str(playerHand) + 'n')
time.sleep(1)
findHandValue()
def findHandValue():
world playerHand
world playerHandValue
# Resets the participant's hand worth to Zero for brand spanking new offers #
playerHandValue = 0
# The worth of the participant's playing cards is decided #
if '2sp' in playerHand:
playerHandValue = playerHandValue + 2
if '3sp' in playerHand:
playerHandValue = playerHandValue + 3
if '4sp' in playerHand:
playerHandValue = playerHandValue + 4
if '5sp' in playerHand:
playerHandValue = playerHandValue + 5
if '6sp' in playerHand:
playerHandValue = playerHandValue + 6
if '7sp' in playerHand:
playerHandValue = playerHandValue + 7
if '8sp' in playerHand:
playerHandValue = playerHandValue + 8
if '9sp' in playerHand:
playerHandValue = playerHandValue + 9
if '10sp' in playerHand:
playerHandValue = playerHandValue + 10
if 'Jsp' in playerHand:
playerHandValue = playerHandValue + 10
if 'Qsp' in playerHand:
playerHandValue = playerHandValue + 10
if 'Ksp' in playerHand:
playerHandValue = playerHandValue + 10
if 'Asp' in playerHand:
playerHandValue = playerHandValue + 11
if '2cl' in playerHand:
playerHandValue = playerHandValue + 2
if '3cl' in playerHand:
playerHandValue = playerHandValue + 3
if '4cl' in playerHand:
playerHandValue = playerHandValue + 4
if '5cl' in playerHand:
playerHandValue = playerHandValue + 5
if '6cl' in playerHand:
playerHandValue = playerHandValue + 6
if '7cl' in playerHand:
playerHandValue = playerHandValue + 7
if '8cl' in playerHand:
playerHandValue = playerHandValue + 8
if '9cl' in playerHand:
playerHandValue = playerHandValue + 9
if '10cl' in playerHand:
playerHandValue = playerHandValue + 10
if 'Jcl' in playerHand:
playerHandValue = playerHandValue + 10
if 'Qcl' in playerHand:
playerHandValue = playerHandValue + 10
if 'Kcl' in playerHand:
playerHandValue = playerHandValue + 10
if 'Acl' in playerHand:
playerHandValue = playerHandValue + 11
if '2he' in playerHand:
playerHandValue = playerHandValue + 2
if '3he' in playerHand:
playerHandValue = playerHandValue + 3
if '4he' in playerHand:
playerHandValue = playerHandValue + 4
if '5he' in playerHand:
playerHandValue = playerHandValue + 5
if '6he' in playerHand:
playerHandValue = playerHandValue + 6
if '7he' in playerHand:
playerHandValue = playerHandValue + 7
if '8he' in playerHand:
playerHandValue = playerHandValue + 8
if '9he' in playerHand:
playerHandValue = playerHandValue + 9
if '10he' in playerHand:
playerHandValue = playerHandValue + 10
if 'Jhe' in playerHand:
playerHandValue = playerHandValue + 10
if 'Qhe' in playerHand:
playerHandValue = playerHandValue + 10
if 'Khe' in playerHand:
playerHandValue = playerHandValue + 10
if 'Ahe' in playerHand:
playerHandValue = playerHandValue + 11
if '2di' in playerHand:
playerHandValue = playerHandValue + 2
if '3di' in playerHand:
playerHandValue = playerHandValue + 3
if '4di' in playerHand:
playerHandValue = playerHandValue + 4
if '5di' in playerHand:
playerHandValue = playerHandValue + 5
if '6di' in playerHand:
playerHandValue = playerHandValue + 6
if '7di' in playerHand:
playerHandValue = playerHandValue + 7
if '8di' in playerHand:
playerHandValue = playerHandValue + 8
if '9di' in playerHand:
playerHandValue = playerHandValue + 9
if '10di' in playerHand:
playerHandValue = playerHandValue + 10
if 'Jdi' in playerHand:
playerHandValue = playerHandValue + 10
if 'Qdi' in playerHand:
playerHandValue = playerHandValue + 10
if 'Kdi' in playerHand:
playerHandValue = playerHandValue + 10
if 'Adi' in playerHand:
playerHandValue = playerHandValue + 11
# Permits Aces to transform from 11 factors to 1 level if the hand worth is over 21 #
if playerHandValue > 21:
if 'Asp' in playerHand:
playerHandValue = playerHandValue - 10
if playerHandValue > 21:
if 'Acl' in playerHand:
playerHandValue = playerHandValue -10
if playerHandValue > 21:
if 'Adi' in playerHand:
playerHandValue = playerHandValue -10
if playerHandValue > 21:
if 'Ahe' in playerHand:
playerHandValue = playerHandValue -10
# Shows the participant's hand worth to the participant #
print("Participant hand worth = " + str(playerHandValue) + 'n')
hitOrStay()
def hitOrStay():
world dealtCard1
world firstDraw
# The seller's first card is revealed to the participant #
if firstDraw == 0:
print('The seller attracts 2 playing cards and divulges ' + str(dealerHand[0]) + 'n')
firstDraw = 1
time.sleep(2)
# If the participant's hand worth is lower than or equal to 21, the participant has the selection to hit or keep #
if playerHandValue <= 21:
hitOrStayChoice = ''
whereas hitOrStayChoice != 'hit' or 'keep':
hitOrStayChoice = enter('Do you 'hit' or 'keep'?' 'n')
if hitOrStayChoice == 'hit':
dealtCard1Number = random.randint(0, len(deck)-1)
dealtCard1 = deck[dealtCard1Number]
playerHand.append(dealtCard1)
del deck[dealtCard1Number]
print('You have been dealt ' + dealtCard1)
findHandValue()
if hitOrStayChoice == 'keep':
revealDealerHand()
# If the participant's hand worth is over 21, the participant loses routinely #
elif playerHandValue > 21:
loseGame()
def revealDealerHand():
world playerHand
world playerHandValue
world dealerHand
world dealerHandValue
dealerHandValue = 0
# The worth of the seller's playing cards is decided in the identical method because the participant's playing cards #
if '2sp' in dealerHand:
dealerHandValue = dealerHandValue + 2
if '3sp' in dealerHand:
dealerHandValue = dealerHandValue + 3
if '4sp' in dealerHand:
dealerHandValue = dealerHandValue + 4
if '5sp' in dealerHand:
dealerHandValue = dealerHandValue + 5
if '6sp' in dealerHand:
dealerHandValue = dealerHandValue + 6
if '7sp' in dealerHand:
dealerHandValue = dealerHandValue + 7
if '8sp' in dealerHand:
dealerHandValue = dealerHandValue + 8
if '9sp' in dealerHand:
dealerHandValue = dealerHandValue + 9
if '10sp' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Jsp' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Qsp' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Ksp' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Asp' in dealerHand:
dealerHandValue = dealerHandValue + 11
if '2cl' in dealerHand:
dealerHandValue = dealerHandValue + 2
if '3cl' in dealerHand:
dealerHandValue = dealerHandValue + 3
if '4cl' in dealerHand:
dealerHandValue = dealerHandValue + 4
if '5cl' in dealerHand:
dealerHandValue = dealerHandValue + 5
if '6cl' in dealerHand:
dealerHandValue = dealerHandValue + 6
if '7cl' in dealerHand:
dealerHandValue = dealerHandValue + 7
if '8cl' in dealerHand:
dealerHandValue = dealerHandValue + 8
if '9cl' in dealerHand:
dealerHandValue = dealerHandValue + 9
if '10cl' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Jcl' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Qcl' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Kcl' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Acl' in dealerHand:
dealerHandValue = dealerHandValue + 11
if '2he' in dealerHand:
dealerHandValue = dealerHandValue + 2
if '3he' in dealerHand:
dealerHandValue = dealerHandValue + 3
if '4he' in dealerHand:
dealerHandValue = dealerHandValue + 4
if '5he' in dealerHand:
dealerHandValue = dealerHandValue + 5
if '6he' in dealerHand:
dealerHandValue = dealerHandValue + 6
if '7he' in dealerHand:
dealerHandValue = dealerHandValue + 7
if '8he' in dealerHand:
dealerHandValue = dealerHandValue + 8
if '9he' in dealerHand:
dealerHandValue = dealerHandValue + 9
if '10he' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Jhe' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Qhe' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Khe' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Ahe' in dealerHand:
dealerHandValue = dealerHandValue + 11
if '2di' in dealerHand:
dealerHandValue = dealerHandValue + 2
if '3di' in dealerHand:
dealerHandValue = dealerHandValue + 3
if '4di' in dealerHand:
dealerHandValue = dealerHandValue + 4
if '5di' in dealerHand:
dealerHandValue = dealerHandValue + 5
if '6di' in dealerHand:
dealerHandValue = dealerHandValue + 6
if '7di' in dealerHand:
dealerHandValue = dealerHandValue + 7
if '8di' in dealerHand:
dealerHandValue = dealerHandValue + 8
if '9di' in dealerHand:
dealerHandValue = dealerHandValue + 9
if '10di' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Jdi' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Qdi' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Kdi' in dealerHand:
dealerHandValue = dealerHandValue + 10
if 'Adi' in dealerHand:
dealerHandValue = dealerHandValue + 11
# this part is to permit Aces to transform from 11 factors to 1 level if the hand worth is over 21 #
if dealerHandValue > 21:
if 'Asp' in dealerHand:
dealerHandValue = dealerHandValue - 10
if dealerHandValue > 21:
if 'Acl' in dealerHand:
dealerHandValue = dealerHandValue -10
if dealerHandValue > 21:
if 'Adi' in dealerHand:
dealerHandValue = dealerHandValue -10
if dealerHandValue > 21:
if 'Ahe' in dealerHand:
dealerHandValue = dealerHandValue -10
# The seller's hand is revealed #
print('n' + 'The seller's hand is ' + str(dealerHand) + ' with a price of ' + str(dealerHandValue) + 'n')
time.sleep(2)
if dealerHandValue <= 16:
dealerHit()
if dealerHandValue > 16:
dealerStay()
def dealerHit():
world dealerHitCard1Number
world dealerHitCard1
world dealerHand
world dealerHitCard1
world dealerHitCard1Number
dealerHitCard1Number = random.randint(0, len(deck)-1)
dealerHitCard1 = deck[dealerHitCard1Number]
dealerHand.append(dealerHitCard1)
del deck[dealerHitCard1Number]
print('The seller hits and attracts ' + dealerHitCard1)
time.sleep(2)
revealDealerHand()
def dealerStay():
if playerHandValue <= dealerHandValue:
if dealerHandValue <= 21:
loseGame()
if playerHandValue > 21:
loseGame()
if dealerHandValue >21 and playerHandValue <= 21:
winGame()
if playerHandValue > dealerHandValue:
if playerHandValue <= 21:
winGame()
if playerHandValue >21:
loseGame()
def loseGame():
world playerHandValue
if playerHandValue <= 21:
print('You lose! Your hand worth was ' + str(playerHandValue) + ', whereas the seller's was ' + str(dealerHandValue) + 'n')
elif playerHandValue > 21:
print('You busted!' + 'n')
askNewGame()
def winGame():
world playerHandValue
world dealerHandValue
print('You gained! Your hand worth was ' + str(playerHandValue) + ', whereas the seller's was ' + str(dealerHandValue) + 'n')
newGame = ''
whereas newGame != 'sure' or 'no':
askNewGame()
def askNewGame():
newGame = enter('Do you need to play one other sport? 'Sure' or 'No'.')
whereas newGame != 'sure' or 'no':
if newGame == 'sure' or 'Sure' or 'y' or 'Y':
dealHand()
if newGame == 'no' or 'No' or 'n' or 'N':
print('Goodbye!')
print('Welcome to Blackjack' + 'n')
enter("Press Enter to deal your first hand.")
dealHand()