Basic coin flipper program - logical error help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DannyB

    #1

    Basic coin flipper program - logical error help

    I'm just learning Python. I've created a simple coin flipper program -
    here is the code:

    [source]
    #Coin flipper
    import random

    heads = 0
    tails = 0
    counter = 0

    coin = random.randrang e(2)

    while (counter < 100):
    if (coin == 0):
    heads += 1
    counter += 1
    else:
    tails += 1
    counter += 1

    coin = random.randrang e(2)


    print "\nThe coin landed on heads", heads, "times."
    print "\nThe coin landed on tails", tails, "times."
    [/source]

    <<<I'm sure the [source] tags don't work - I through them in there
    anyway.>>>

    The program runs - however - it will give me 100 heads OR 100 tails.
    Can someone spot the logic error?

    Thanks

    ~Dan

  • John McMonagle

    #2
    Re: Basic coin flipper program - logical error help

    On Tue, 2006-02-21 at 16:14 -0800, DannyB wrote:[color=blue]
    > I'm just learning Python. I've created a simple coin flipper program -
    > here is the code:
    >
    > [source]
    > #Coin flipper
    > import random
    >
    > heads = 0
    > tails = 0
    > counter = 0
    >
    > coin = random.randrang e(2)
    >
    > while (counter < 100):
    > if (coin == 0):
    > heads += 1
    > counter += 1
    > else:
    > tails += 1
    > counter += 1
    >
    > coin = random.randrang e(2)
    >
    >
    > print "\nThe coin landed on heads", heads, "times."
    > print "\nThe coin landed on tails", tails, "times."
    > [/source]
    >
    > <<<I'm sure the [source] tags don't work - I through them in there
    > anyway.>>>
    >
    > The program runs - however - it will give me 100 heads OR 100 tails.
    > Can someone spot the logic error?[/color]

    Yes. Put coin = random.randrang e(2) inside the while loop.

    import random

    heads = 0
    tails = 0
    counter = 0

    coin = random.randrang e(2)

    while (counter < 100):
    if (coin == 0):
    heads += 1
    counter += 1
    else:
    tails += 1
    counter += 1

    coin = random.randrang e(2)

    print "\nThe coin landed on heads", heads, "times."
    print "\nThe coin landed on tails", tails, "times."


    --
    This message has been scanned for viruses and
    dangerous content by MailScanner, and is
    believed to be clean.

    Comment

    • Claudio Grondi

      #3
      Re: Basic coin flipper program - logical error help

      DannyB wrote:[color=blue]
      > I'm just learning Python. I've created a simple coin flipper program -
      > here is the code:
      >
      > [source]
      > #Coin flipper
      > import random
      >
      > heads = 0
      > tails = 0
      > counter = 0
      >
      > while (counter < 100):[/color]

      coin = random.randrang e(2)

      Claudio
      [color=blue]
      > if (coin == 0):
      > heads += 1
      > counter += 1
      > else:
      > tails += 1
      > counter += 1
      >
      > coin = random.randrang e(2)
      >
      >
      > print "\nThe coin landed on heads", heads, "times."
      > print "\nThe coin landed on tails", tails, "times."
      > [/source]
      >
      > <<<I'm sure the [source] tags don't work - I through them in there
      > anyway.>>>
      >
      > The program runs - however - it will give me 100 heads OR 100 tails.
      > Can someone spot the logic error?
      >
      > Thanks
      >
      > ~Dan
      >[/color]

      Comment

      • Larry Bates

        #4
        Re: Basic coin flipper program - logical error help

        DannyB wrote:[color=blue]
        > I'm just learning Python. I've created a simple coin flipper program -
        > here is the code:
        >
        > [source]
        > #Coin flipper
        > import random
        >
        > heads = 0
        > tails = 0
        > counter = 0
        >
        > coin = random.randrang e(2)
        >
        > while (counter < 100):
        > if (coin == 0):
        > heads += 1
        > counter += 1
        > else:
        > tails += 1
        > counter += 1
        >
        > coin = random.randrang e(2)
        >
        >
        > print "\nThe coin landed on heads", heads, "times."
        > print "\nThe coin landed on tails", tails, "times."
        > [/source]
        >
        > <<<I'm sure the [source] tags don't work - I through them in there
        > anyway.>>>
        >
        > The program runs - however - it will give me 100 heads OR 100 tails.
        > Can someone spot the logic error?
        >
        > Thanks
        >
        > ~Dan
        >[/color]
        Looks an awful lot like your homework, but I'll give you a clue.
        You need to get the your coin tosses inside your loop. Otherwise
        you only toss the coin once and then loop 100 times with the
        same value.

        -Larry Bates

        Comment

        • wes weston

          #5
          Re: Basic coin flipper program - logical error help

          DannyB wrote:[color=blue]
          > I'm just learning Python. I've created a simple coin flipper program -
          > here is the code:
          >
          > [source]
          > #Coin flipper
          > import random
          >
          > heads = 0
          > tails = 0
          > counter = 0
          >
          > coin = random.randrang e(2)
          >
          > while (counter < 100):
          > if (coin == 0):
          > heads += 1
          > counter += 1
          > else:
          > tails += 1
          > counter += 1
          >
          > coin = random.randrang e(2)
          >
          >
          > print "\nThe coin landed on heads", heads, "times."
          > print "\nThe coin landed on tails", tails, "times."
          > [/source]
          >
          > <<<I'm sure the [source] tags don't work - I through them in there
          > anyway.>>>
          >
          > The program runs - however - it will give me 100 heads OR 100 tails.
          > Can someone spot the logic error?
          >
          > Thanks
          >
          > ~Dan
          >[/color]

          Dan,
          Looping is easier with:
          for x in range(100):
          if random.randint( 0,1) == 0:
          heads += 1
          else:
          tails += 1

          Inside the loop you need to "flip" on each pass.
          You're "flipping" once before the start of the loop now.
          wes

          Comment

          • Martin P. Hellwig

            #6
            Re: Basic coin flipper program - logical error help

            DannyB wrote:[color=blue]
            > I'm just learning Python.[/color]

            So am I :-)
            [color=blue]
            > I've created a simple coin flipper program -
            > here is the code:
            >
            > [source]
            > #Coin flipper
            > import random
            >
            > heads = 0
            > tails = 0
            > counter = 0
            >
            > coin = random.randrang e(2)
            >
            > while (counter < 100):
            > if (coin == 0):
            > heads += 1
            > counter += 1
            > else:
            > tails += 1
            > counter += 1
            >
            > coin = random.randrang e(2)[/color]

            This line is you logic error because it's not part of your while loop
            the coin variables get the result of random.randrang e(2) assigned only
            one time (before the loop).
            [color=blue]
            >
            >
            > print "\nThe coin landed on heads", heads, "times."
            > print "\nThe coin landed on tails", tails, "times."
            > [/source]
            >
            > <<<I'm sure the [source] tags don't work - I through them in there
            > anyway.>>>
            >
            > The program runs - however - it will give me 100 heads OR 100 tails.
            > Can someone spot the logic error?
            >
            > Thanks
            >
            > ~Dan
            >[/color]

            You could changed the program to this it works too and is just as
            readable (IMHO):

            #Coin flipper
            import random

            heads = 0
            tails = 0
            counter = 0
            # removed random line
            while (counter < 100):
            if random.randrang e(2): # put random here
            heads += 1
            counter += 1
            else:
            tails += 1
            counter += 1
            # removed random line
            print "\nThe coin landed on heads", heads, "times."
            print "\nThe coin landed on tails", tails, "times."

            Take my advice with caution I'm also new to this :-)

            Btw, it is possible that the coins lands on it side if not catched with
            the hand (yes I have seen it happen) ;-)

            --
            mph

            Comment

            • DannyB

              #7
              Re: Basic coin flipper program - logical error help

              Thanks everyone for your insight.

              I'm coming from C++ - I'm used to formatting code with {} instead of
              whitespaces.

              @Larry - this isn't my homework :P I'm actually taking a VB.NET class
              in school.

              I was teaching myself C++ but decided to scale back to Python. I've
              heard it was a bit easier to understand and it cuts your development
              time by at least 50% (I've heard 90%).

              Logically I can figure things out - its the formatting of the logic in
              Python that is messing me up. I'll get it soon enough =)

              Comment

              • John Zenger

                #8
                Re: Basic coin flipper program - logical error help

                wes weston wrote:[color=blue]
                > Looping is easier with:
                > for x in range(100):
                > if random.randint( 0,1) == 0:
                > heads += 1
                > else:
                > tails += 1[/color]

                Also, with the functional programming tools of map, filter, and lambda,
                this code can be reduced to just six lines:

                import random

                flips = map(lambda x: random.randrang e(2), xrange(100))
                heads = len(filter(lamb da x: x is 0, flips))
                tails = len(filter(lamb da x: x is not 0, flips))

                print "The coin landed on heads", heads, "times."
                print "The coin landed on tails", tails, "times."

                Comment

                • bonono@gmail.com

                  #9
                  Re: Basic coin flipper program - logical error help


                  John Zenger wrote:[color=blue]
                  > Also, with the functional programming tools of map, filter, and lambda,
                  > this code can be reduced to just six lines:
                  >
                  > import random
                  >
                  > flips = map(lambda x: random.randrang e(2), xrange(100))
                  > heads = len(filter(lamb da x: x is 0, flips))
                  > tails = len(filter(lamb da x: x is not 0, flips))[/color]

                  Or a filter/map/lambda free way:

                  heads = sum(random.rand range(2) for x in xrange(100))
                  tails = 100 - heads

                  Comment

                  • Paul McGuire

                    #10
                    Re: Basic coin flipper program - logical error help

                    <bonono@gmail.c om> wrote in message
                    news:1140578858 .586591.243900@ g43g2000cwa.goo glegroups.com.. .[color=blue]
                    >
                    > John Zenger wrote:[color=green]
                    > > Also, with the functional programming tools of map, filter, and lambda,
                    > > this code can be reduced to just six lines:
                    > >
                    > > import random
                    > >
                    > > flips = map(lambda x: random.randrang e(2), xrange(100))
                    > > heads = len(filter(lamb da x: x is 0, flips))
                    > > tails = len(filter(lamb da x: x is not 0, flips))[/color]
                    >
                    > Or a filter/map/lambda free way:
                    >
                    > heads = sum(random.rand range(2) for x in xrange(100))
                    > tails = 100 - heads
                    >[/color]
                    sort, then groupby.


                    import itertools
                    import random
                    h,t = [len(list(g)) for k,g in itertools.group by(sorted([random.randrang e(2)
                    for i in xrange(100)]))]
                    print h,t



                    Comment

                    • Jeffrey Schwab

                      #11
                      Re: Basic coin flipper program - logical error help

                      wes weston wrote:[color=blue]
                      > DannyB wrote:
                      >[color=green]
                      >> I'm just learning Python. I've created a simple coin flipper program -[/color][/color]

                      ....
                      [color=blue]
                      > Dan,
                      > Looping is easier with:
                      > for x in range(100):
                      > if random.randint( 0,1) == 0:
                      > heads += 1
                      > else:
                      > tails += 1
                      >[/color]

                      Or, continuing with that theme:

                      for x in range(N):
                      heads += random.randint( 0, 1)

                      As in:

                      import random
                      N = 100
                      heads = 0
                      for x in range(N):
                      heads += random.randint( 0, 1)
                      print "%d heads and %d tails." % (heads, N - heads)

                      Comment

                      • Brian van den Broek

                        #12
                        Re: Basic coin flipper program - logical error help

                        DannyB said unto the world upon 21/02/06 06:14 PM:[color=blue]
                        > I'm just learning Python. I've created a simple coin flipper program -
                        > here is the code:
                        >
                        > [source]
                        > #Coin flipper
                        > import random
                        >
                        > heads = 0
                        > tails = 0
                        > counter = 0
                        >
                        > coin = random.randrang e(2)
                        >
                        > while (counter < 100):
                        > if (coin == 0):
                        > heads += 1
                        > counter += 1
                        > else:
                        > tails += 1
                        > counter += 1
                        >
                        > coin = random.randrang e(2)[/color]

                        <snip>

                        [color=blue]
                        > The program runs - however - it will give me 100 heads OR 100 tails.
                        > Can someone spot the logic error?[/color]

                        <snip>

                        Your original question is long since answered. But I've a style point.
                        As Dennis Lee Bieber pointed out, you don't need all three
                        accumulators. If you keep to the overall style of your code, you can
                        avoid repeating yourself as:


                        while (counter < 100):
                        counter += 1 # No point in putting this in each branch
                        coin = random.randrang e(2)
                        if (coin == 0):
                        heads += 1
                        else:
                        tails += 1


                        For roughly the same style, I'd go with:

                        heads = 0
                        count = 100

                        for i in range(count):
                        if random.randrang e(2):
                        heads += 1

                        tails = count - heads

                        HTH,

                        Brian vdB

                        Comment

                        • Paul McGuire

                          #13
                          Re: Basic coin flipper program - logical error help


                          "Paul McGuire" <ptmcg@austin.r r._bogus_.com> wrote in message
                          news:yURKf.2275 0$UN2.3873@torn ado.texas.rr.co m...[color=blue]
                          > <bonono@gmail.c om> wrote in message
                          > news:1140578858 .586591.243900@ g43g2000cwa.goo glegroups.com.. .[/color]
                          [color=blue]
                          > sort, then groupby.
                          >
                          >
                          > import itertools
                          > import random
                          > h,t = [len(list(g)) for k,g in[/color]
                          itertools.group by(sorted([random.randrang e(2)[color=blue]
                          > for i in xrange(100)]))]
                          > print h,t
                          >
                          >[/color]
                          By the way, sort + groupby generalizes beyond just coin-flipping. Here is a
                          modified version that simulates die rolls.

                          -- Paul

                          import itertools
                          import random

                          NUM_ROLLS = 1000
                          dieRolls = [random.randrang e(6)+random.ran drange(6)+2 for i in
                          xrange(NUM_ROLL S)]

                          # create dummy list entries for impossible rolls of 0 and 1
                          rolls = [None,None]

                          rolls += [len(list(g)) for k,g in itertools.group by(sorted(dieRo lls))]

                          # print out nice histogram
                          for i,r in enumerate(rolls ):
                          if i > 1:
                          print "%2d - %s" % (i,"*"*int(roun d(r/10.0)))


                          prints:

                          2 - ***
                          3 - *****
                          4 - *********
                          5 - **********
                          6 - ***************
                          7 - *************** ***
                          8 - **************
                          9 - *********
                          10 - *******
                          11 - *******
                          12 - ***



                          Comment

                          Working...