#Shuffle the deck and deal 2 cards #Check if you have blackjack #Repeat process until you have blackjack #How many times did you repeat the process until you had blackjack #Below is the Udemy professor's solution def shuffleDeck(): cardDeck = ['A','K','Q','J','10','9','8','7','6','5','4','3','2', 'A','K','Q','J','10','9','8','7','6','5','4','3','2', 'A','K','Q','J','10','9','8','7','6','5','4','3','2', 'A','K','Q','J','10','9','8','7','6','5','4','3','2'] returnDeck = [] # Empty list will be filled with random order of cards noCardsInDeck = 52 import random while noCardsInDeck > 0: # when there aren't any cards left in deck it will exit loop x = random.randint(0, noCardsInDeck-1) xCard = cardDeck[x] returnDeck.append(xCard) cardDeck.pop(x) # removes xCard from cardDeck noCardsInDeck = noCardsInDeck - 1 # Must decrease this so the x is not out of range return returnDeck # output of function is shuffled deck def getcardValue(card): # calculates value of hand based on 11 for A and 10 for K,Q,J,10. 0 is for the rest of the cards. if card == "A": return 11 if card == "K" or card == "Q" or card == "J" or card == "10": return 10 return 0 total = 0 # calculates the total of the hand count = 0 # counts how many hands until blackjack while total != 21: # loop exit when blackjack (21) is found count += 1 x = shuffleDeck() # here we assign the output of shuffleDeck() which is a shuffled deck to x total = getcardValue(x[0]) + getcardValue(x[1]) # here we use our 2 functions combined to get total of 2 cards print "Number of deals until blackjack = " + str(count)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question