Flip coin problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coolindienc
    New Member
    • Aug 2006
    • 48

    #1

    Flip coin problem

    At first I had no idea where to start. Finally I tried to get somewhere and I am still stuck. I wrote two different ways using while loop and for loop. Below are codes for both. I am getting errors different places for each loop.

    For this code I am getting error at "else" statement.

    Code:
    import random
    heads = tails = count = 0
    while count <= 100:
        flip = random.randrange(2)
        if flip == 0:
            heads += 1
            [B]else:[/B]
                tails += 1
                count += 1
    print "The coint landed on heads",heads,'times' \ " and on tails ", tails, 'times'
    raw_input ("\nEnd of program...Press enter to exit.")  
    
    [B]For this I am getting error at colon on for loop at third line.[/B]
    
    import random
    heads = tails = count = 0
    [B]for (count != 100):[/B]
        flip = random.randrange(2)
        if flip == 0:
            heads += 1
            else:
                tails += 1
                count += 1
    print "The coint landed on heads",heads,'times' \ " and on tails ", tails, 'times'
    raw_input ("\nEnd of program...Press enter to exit.")
    Someone please help
    Last edited by bartonc; Dec 4 '06, 11:17 PM. Reason: added code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by coolindienc
    At first I had no idea where to start. Finally I tried to get somewhere and I am still stuck. I wrote two different ways using while loop and for loop. Below are codes for both. I am getting errors different places for each loop.

    For this code I am getting error at "else" statement.
    Code:
    import random
    heads = tails = count = 0
    while count <= 100:
        flip = random.randrange(2)
        if flip == 0:
            heads += 1
            [B]else:[/B]
                tails += 1
                count += 1
    print "The coint landed on heads",heads,'times' \ " and on tails ", tails, 'times'
    raw_input ("\nEnd of program...Press enter to exit.")  
    
    [B]For this I am getting error at colon on for loop at third line.[/B]
    
    import random
    heads = tails = count = 0
    [B]for (count != 100):[/B]
        flip = random.randrange(2)
        if flip == 0:
            heads += 1
            else:
                tails += 1
                count += 1
    print "The coint landed on heads",heads,'times' \ " and on tails ", tails, 'times'
    raw_input ("\nEnd of program...Press enter to exit.")
    Someone please help
    It would help if you would enclose your code in code tags. See the reply guidelines.

    I think you have an indentation error at the else statement.

    There was a problem in your print statement. I took the liberty of rewriting it using string formatting.
    Code:
    print "The coin landed on heads %s and on tails %s times" % (heads, tails)
    Last edited by bartonc; Dec 5 '06, 12:06 AM. Reason: added code tags

    Comment

    • coolindienc
      New Member
      • Aug 2006
      • 48

      #3
      Thanks again bvdet,
      It's funny how this language works. I took back "else" a tab back and it worked. However, my flip count exceeds 100 on heads and tail. Andy suggestions??

      Andy

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by coolindienc
        Thanks again bvdet,
        It's funny how this language works. I took back "else" a tab back and it worked. However, my flip count exceeds 100 on heads and tail. Andy suggestions??

        Andy
        You are incrementing 'count' at an incorrect indentation.

        Comment

        • coolindienc
          New Member
          • Aug 2006
          • 48

          #5
          That is what I suspected at first but I tried everything possible. undoftunately I supposed I have not tried only thing that does work. I even tried to changing location, but at the end I thought this was the best place to put code. I am getting frustrated but I am not goving up on this. I guess this is the only way to learn, huh? : )

          Andy

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by coolindienc
            That is what I suspected at first but I tried everything possible. undoftunately I supposed I have not tried only thing that does work. I even tried to changing location, but at the end I thought this was the best place to put code. I am getting frustrated but I am not goving up on this. I guess this is the only way to learn, huh? : )

            Andy
            I have learned most of what I know by:
            • Listening to others that know more than me
            • Screwing up
            • Trying new things until something works

            But not necessarily in that order! Not giving up is required.
            Keep it up Andy. The light will come on one day.

            Comment

            • kudos
              Recognized Expert New Member
              • Jul 2006
              • 127

              #7
              Isn't it simpler to do the following?
              Code:
              import random
              
              heads = 0
              for i in range(100):
               heads+=random.randrange(2)
              
              print str(heads) + " " + str(100 - heads)
              -kudos


              Originally posted by coolindienc
              That is what I suspected at first but I tried everything possible. undoftunately I supposed I have not tried only thing that does work. I even tried to changing location, but at the end I thought this was the best place to put code. I am getting frustrated but I am not goving up on this. I guess this is the only way to learn, huh? : )

              Andy

              Comment

              • coolindienc
                New Member
                • Aug 2006
                • 48

                #8
                Hmmm...... I never knew it will be as simple as this. Thanks kudos

                Andy

                Comment

                • melina386
                  New Member
                  • Nov 2009
                  • 2

                  #9
                  nice discussion and its very useful to understand about coin flip thanx for posting keep posting
                  ----------------------------------------
                  <removed link>
                  Last edited by Frinavale; Dec 8 '09, 05:17 PM. Reason: Removed link. Link had no relevance to problem.

                  Comment

                  Working...