Python Forum
I am getting an incorrect average, and not sure why? What's wrong with my code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am getting an incorrect average, and not sure why? What's wrong with my code?
#8
Your big error is in oneGame()
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 countFlips
Your 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
Reply


Messages In This Thread
RE: I am getting an incorrect average, and not sure why? What's wrong with my code? - by deanhystad - Nov-19-2020, 07:51 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  im not sure what ive done wrong code doesnt run dgizzly 3 2,230 Nov-16-2022, 03:02 AM
Last Post: deanhystad
  what is wrong with my code 53535 4 2,409 Apr-07-2022, 11:37 AM
Last Post: 53535
  What's wrong with my code? NeedHelpPython 4 3,100 Oct-22-2021, 07:59 PM
Last Post: Yoriz
  Help with my code due 11:59 pm, can you tell me where I went wrong and help fix it? shirleylam852 1 3,371 Dec-09-2020, 06:37 AM
Last Post: stranac
  Am I wrong? Or is the question setup wrong? musicjoeyoung 3 3,473 May-18-2020, 03:38 PM
Last Post: musicjoeyoung
  Something is Wrong with my code susmith552 4 4,003 Nov-28-2019, 02:16 AM
Last Post: susmith552
  What is wrong with my code? Than999 1 3,140 Nov-10-2019, 08:59 PM
Last Post: ichabod801
  Wrong output on my code. JTNA 2 9,217 Apr-04-2019, 01:55 PM
Last Post: JTNA
  Return giving incorrect value Calingaladha 5 4,616 Oct-24-2018, 09:53 PM
Last Post: LeSchakal
  Why is this code wrong? Lemmy 4 6,229 Apr-05-2018, 03:46 PM
Last Post: Lemmy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020