Nov-19-2020, 07:51 AM
Your big error is in oneGame()
The for/else is a bit confusing. I don't think any other programming language has this logic construct. You might find the code easier to understand if the "hand" logic is broken out into it's own function.
def oneGame(initial): countFlips = 0 bankroll = initial while 0 < bankroll < 2*initial: # This is where you error was. You did multiple # adds and subtracts for each hand. There should # only be 1 ad or subtract for card in shuffledDeck[0:6]: # Look at top six cards if 'ace' in card: bankroll += 1 break # Stop after find first ace else: bankroll -= 1 # Only do this if for loop completes countFlips += 1 return countFlipsYour code added a dollar for each ace and subtracted a dollar for each non-ace. If you drew 4 aces you would add $2, but if you drew no aces you lost $6. In the code above we break out of the for loop after finding the first ace. The "else" statement is only executed when there are no aces in the hand.
The for/else is a bit confusing. I don't think any other programming language has this logic construct. You might find the code easier to understand if the "hand" logic is broken out into it's own function.
def hand(): for card in shuffledDeck[0:6]: # Look at top 6 cards if 'ace' in card: return 1 # Return 1 if we find an ace return -1 # Return -1 if no ace found def oneGame(initial): countFlips = 0 bankroll = initial while 0 < bankroll < 2*initial: bankroll += hand() countFlips += 1 return countFlips