Need help coding?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ccgrl451
    New Member
    • Nov 2009
    • 8

    Need help coding?

    Okay, just to let you know, I am a certified noob, so I'm not really sure what to do. Here's what I"m supposed to do. Write a function three_heads that stimulates flipping a coin repeatedly printing what is seen (H for heads, T for tails). When 3 heads are flipped in a row, a message is printed. Here is a sample output:

    T
    H
    T
    H
    H
    H
    Three Heads in a row!



    This isn't homework, but a review question that we didn't go over in class. Just want to satisfy my curious mind. Any possible answers? Thanks.

    I'm thinking that it should be like:
    Code:
    # random chooses Heads or tails
    from random import*
     
    def three_heads():
    while(True):
         possiblity=ranint(1,2)
         if (possibilty==1):
                print ("H")
         elif (possibility==2):
                print ("T")

    But now I'm stuck. Any ideas? Not asking for solution, just help/hints.
    Last edited by bvdet; Nov 25 '09, 04:05 PM. Reason: Add code tags
  • nicstel
    New Member
    • Jun 2008
    • 20

    #2
    A lot of solutions is possible to do what you want to do. Try this one and tell me if you understand the function that I have built.

    Code:
    import random
    import string
    
    first_time = ""
    second_time = ""
    third_time = ""
    
    def head_or_tails():
        A = random.choice(["Head", "Tails"])
        return A
    
    for B in range(100):
        C = head_or_tails()
        print C
        if C == first_time:
            second_time = C
            if C == second_time:
                third_time = C
                if C == third_time:
                    first_time = ""
                    second_time = ""
                    third_time = ""
                    print "Three " + str(C) + " in a row!"
        first_time = C

    Comment

    • ccgrl451
      New Member
      • Nov 2009
      • 8

      #3
      Well, I understand some of it, but could you explain each line. More specifically, line 3(why do you need to import string?),line 11 (what does "return A do?), and the whole for loop. Thanks!

      Comment

      • nicstel
        New Member
        • Jun 2008
        • 20

        #4
        ccgrl451,

        line 3: In this case is not necessary to import string. I use it often. But if you want to remove import string you need to fix line 24. Replace str(C) by C.

        line 11: When you want to extract information from a function you need to do a return of the answer. A = Head or Tails.

        loop: I have create three variables. first_time, second_time and third_time. When these variables are identical, it's mean that you have three Head or Tails in row.

        You have a lot of possibilities. I have simplify the code.

        Code:
        import random
        
        first_time = ""
        second_time = ""
        third_time = ""
        
        for B in range(10):
            C = random.choice(["Head", "Tails"])
            print C
            if C == first_time:
                second_time = C
                if C == second_time:
                    third_time = C
                    if C == third_time:
                        first_time = ""
                        second_time = ""
                        third_time = ""
                        print C
                        print "Three " + C + " in a row!"
            first_time = C

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Following is another possibility:
          Code:
          import random
          
          tosses = 'XXX'
          dd = {"H": "Heads", "T": "Tails"}
          while True:
              toss = random.choice(["H", "T"])
              tosses += toss
              tosses = tosses[1:]
              print toss
              if tosses in ["HHH", 'TTT']:
                  print 'Three %s in a row!' % (dd[tosses[0]])
                  break

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

            #6
            Hey. You were really close actually. You had the random generator going.

            All you needed was to keep track of the throws over time and then break out of the loop when you had success.

            So add a tosses="" above your while loop.
            Add a tosses+="H" above or below print("H") and similarly for "T"
            Add a if "TTT" in tosses: break

            And you're done!

            Comment

            Working...