beginner code problem

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

    #1

    beginner code problem

    I'm trying to teach myself Python (probably running into the old dog
    new tricks issue) and I'm trying to start with the very basics to get a
    handle on them.

    I'm trying to write code to get the computer to flip a coin 100 times
    and give me the output of how many times heads and tails. After solving
    a few syntax errors I seem to be stuck in an endless loop and have to
    kill python. A few times I would get it to print 'heads 0 (or 1) times
    and tails 1 (or 0) times' 100 times.

    Here's the code I wrote:

    import random

    flip = random.randrang e(2)
    heads = 0
    tails = 0
    count = 0

    while count < 100:

    if flip == 0:
    heads += 1

    else:
    tails += 1


    count += 1



    print "The coin landed on heads", heads, 'times ' \
    "and tails", tails, 'times'

  • John Machin

    #2
    Re: beginner code problem

    On 3/06/2006 8:41 AM, RJ wrote:[color=blue]
    > I'm trying to teach myself Python (probably running into the old dog
    > new tricks issue) and I'm trying to start with the very basics to get a
    > handle on them.
    >
    > I'm trying to write code to get the computer to flip a coin 100 times
    > and give me the output of how many times heads and tails. After solving
    > a few syntax errors I seem to be stuck in an endless loop and have to
    > kill python.[/color]

    This can't happen with the code that you posted. It *could* happen if
    the statement count += 1 was not being executed once each time around
    the while loop -- like if it was *not* indented.

    # Bad code #1
    import random
    flip = random.randrang e(2)
    heads = tails = count = 0
    while count < 100:
    if flip == 0:
    heads += 1
    else:
    tails += 1
    count += 1
    print "The coin landed on heads", heads, 'times ' \
    "and tails", tails, 'times'
    [color=blue]
    > A few times I would get it to print 'heads 0 (or 1) times
    > and tails 1 (or 0) times' 100 times.[/color]

    Again, can't happen with the code you have posted. If it is printing 100
    times, that would be because you have indented the print statement so
    that it is being executed once each trip around the loop.

    # Bad code #2
    import random
    flip = random.randrang e(2)
    heads = tails = count = 0
    while count < 100:
    if flip == 0:
    heads += 1
    else:
    tails += 1
    count += 1
    print "The coin landed on heads", heads, 'times ' \
    "and tails", tails, 'times'
    [color=blue]
    >
    > Here's the code I wrote:
    >
    > import random
    >
    > flip = random.randrang e(2)
    > heads = 0
    > tails = 0
    > count = 0
    >
    > while count < 100:[/color]

    To help you see what is happening, insert a print statement here; e.g.:
    print flip, count, heads, tails[color=blue]
    >
    > if flip == 0:
    > heads += 1
    >
    > else:
    > tails += 1
    >
    >
    > count += 1
    >
    >
    >
    > print "The coin landed on heads", heads, 'times ' \
    > "and tails", tails, 'times'
    >[/color]

    The code that you posted sets flip only once i.e. only 1 toss, not 100.
    If it is 0, you get 100 heads and 0 tails. Otherwise you get 0 heads and
    100 tails. You need to get a new value for flip each trip.

    # Not quite so bad code
    import random
    heads = tails = count = 0
    while count < 100:
    flip = random.randrang e(2)
    # print flip, count, heads, tails # un-comment as/when required :-)
    if flip == 0:
    heads += 1
    else:
    tails += 1
    count += 1
    print "The coin landed on heads", heads, 'times ' \
    "and tails", tails, 'times'

    HTH,
    John

    Comment

    • Schüle Daniel

      #3
      Re: beginner code problem

      Hello
      [color=blue]
      > Here's the code I wrote:
      >
      > import random
      >
      > flip = random.randrang e(2)
      > heads = 0
      > tails = 0
      > count = 0
      >
      > while count < 100:
      >
      > if flip == 0:[/color]

      flip never changes again
      it's not reassigned in the while loop
      [color=blue]
      > heads += 1
      >
      > else:
      > tails += 1
      >
      >
      > count += 1
      >
      >
      >
      > print "The coin landed on heads", heads, 'times ' \
      > "and tails", tails, 'times'
      >[/color]


      in case you know how many times to iterate
      it's better to use for loop (in python and eq C also)

      from random import randrange as flip
      result = [0,0]
      for i in range(100):
      result[flip(2)] += 1

      or

      from random import randrange as flip
      result = {"head":0,
      "tail":0}
      for i in range(100):
      result[["head","tai l"]flip(2)] += 1

      or
      [color=blue][color=green][color=darkred]
      >>> class Coin:[/color][/color][/color]
      .... def flip(self):
      .... import random
      .... return ("head", "tail")[random.randrang e(2)]
      c = Coin()
      result = {"head":0,"tail ":0}
      for i in range(100):
      result[c.flip()] += 1

      or many many more
      the important thing is .. to know what is the
      most suitable data representation for you

      is it throw-away-code or is this going to be read
      by other people .. etc

      hth, Daniel

      Comment

      • DaveM

        #4
        Re: beginner code problem

        On Fri, 2 Jun 2006 18:41:53 -0400, RJ <victimorcrime@ yahoo.com> wrote:
        [color=blue]
        > I'm trying to teach myself Python (probably running into the old dog
        >new tricks issue) and I'm trying to start with the very basics to get a
        >handle on them.
        >
        > I'm trying to write code to get the computer to flip a coin 100 times
        >and give me the output of how many times heads and tails. After solving
        >a few syntax errors I seem to be stuck in an endless loop and have to
        >kill python. A few times I would get it to print 'heads 0 (or 1) times
        >and tails 1 (or 0) times' 100 times.
        >
        >Here's the code I wrote:
        >
        >import random
        >
        >flip = random.randrang e(2)
        >heads = 0
        >tails = 0
        >count = 0
        >
        >while count < 100:
        >
        > if flip == 0:
        > heads += 1
        >
        > else:
        > tails += 1
        >
        >
        > count += 1
        >
        >
        >
        >print "The coin landed on heads", heads, 'times ' \
        > "and tails", tails, 'times'[/color]

        Several problems here. "flip" is defined just once, so you'll either have
        100 heads or tails and your whitespace is all wrong - that's important in
        Python. Here's how it should look:

        import random

        def coinflip():
        heads = 0
        tails = 0
        for goes in range(100):
        if random.randrang e(2) == 1:
        heads += 1
        else:
        tails += 1
        print "heads", heads
        print "tails", tails

        if __name__ == "__main__":
        coinflip()

        Comment

        • Rob Johnson

          #5
          Re: beginner code problem

          On 2006-06-02 19:25:28 -0400, John Machin <sjmachin@lexic on.net> said:
          [color=blue]
          > Thanks for the reply John. I seem to be getting all the same problems
          > with your code that I had with mine so it may be an issue with Python
          > on this computer though I haven't had one prior to now. I'm running it
          > on OSX Tiger so I'll give it a shot on my Windows box. With pythons
          > portability I didn't think it would be an issue so didn't mention it in
          > the post.[/color]

          The different problems I was having was a result of moving some of
          the code around thinking I had messed up and trying to fix it. The code
          I posted was just running but never stopping to give the the output.

          Thanks for explaining about getting a new value for flip, I wasn't
          positive about that and you really helped clear up any confusion I was
          having.

          Rob
          [color=blue]
          >
          > This can't happen with the code that you posted. It *could* happen if
          > the statement count += 1 was not being executed once each time around
          > the while loop -- like if it was *not* indented.
          >
          > # Bad code #1
          > import random
          > flip = random.randrang e(2)
          > heads = tails = count = 0
          > while count < 100:
          > if flip == 0:
          > heads += 1
          > else:
          > tails += 1
          > count += 1
          > print "The coin landed on heads", heads, 'times ' \
          > "and tails", tails, 'times'
          >[color=green]
          >> A few times I would get it to print 'heads 0 (or 1) times and tails 1
          >> (or 0) times' 100 times.[/color]
          >
          > Again, can't happen with the code you have posted. If it is printing
          > 100 times, that would be because you have indented the print statement
          > so that it is being executed once each trip around the loop.
          >
          > # Bad code #2
          > import random
          > flip = random.randrang e(2)
          > heads = tails = count = 0
          > while count < 100:
          > if flip == 0:
          > heads += 1
          > else:
          > tails += 1
          > count += 1
          > print "The coin landed on heads", heads, 'times ' \
          > "and tails", tails, 'times'
          >[color=green]
          >>
          >> Here's the code I wrote:
          >>
          >> import random
          >>
          >> flip = random.randrang e(2)
          >> heads = 0
          >> tails = 0
          >> count = 0
          >>
          >> while count < 100:[/color]
          >
          > To help you see what is happening, insert a print statement here; e.g.:
          > print flip, count, heads, tails[color=green]
          >>
          >> if flip == 0:
          >> heads += 1
          >> else:
          >> tails += 1
          >>
          >> count += 1
          >>
          >> print "The coin landed on heads", heads, 'times ' \
          >> "and tails", tails, 'times'
          >>[/color]
          >
          > The code that you posted sets flip only once i.e. only 1 toss, not 100.
          > If it is 0, you get 100 heads and 0 tails. Otherwise you get 0 heads
          > and 100 tails. You need to get a new value for flip each trip.
          >
          > # Not quite so bad code
          > import random
          > heads = tails = count = 0
          > while count < 100:
          > flip = random.randrang e(2)
          > # print flip, count, heads, tails # un-comment as/when required :-)
          > if flip == 0:
          > heads += 1
          > else:
          > tails += 1
          > count += 1
          > print "The coin landed on heads", heads, 'times ' \
          > "and tails", tails, 'times'
          >
          > HTH,
          > John[/color]


          Comment

          • Rene Pijlman

            #6
            Re: beginner code problem

            RJ:[color=blue]
            >import random
            >flip = random.randrang e(2)
            >heads = 0
            >tails = 0
            >count = 0
            >while count < 100:
            > if flip == 0:
            > heads += 1
            > else:
            > tails += 1
            > count += 1[/color]

            Since flip isn't changed in the loop, this is going to report 100 heads or
            100 tails, depending on the zeroness of flip.

            --
            René Pijlman

            Comment

            • RJ

              #7
              Re: beginner code problem

              Thanks for all the help and examples. As I mentioned, I'm trying to
              teach myself and starting with the basics (if, elif, else, while, for,
              raw_input, int are about all I know so far and don't know that well
              yet) Some of the example posted are still beyond my understanding but
              it's good to see other ways of acheiving the result and other things
              to look up and read about.

              I've read a bit about Python but only really started trying
              yesterday. Right now I'm still strugling but with practice I hope for
              it to come easier and I'll keep learning more syntax.

              I didn't even think about random.randrang e being out of the loop but
              it makes sence now. Chalk it up to a newbie mistake and a learning
              experience.

              I really do appreciate the help and I'm sure this is only the first
              of many questions I'll have. It's good to know that there are others
              who can help me understand my mistakes.

              Rob

              Comment

              Working...