0

I have written a small coin flipping program for Home work Python, it will choose one of two values; Heads or Tails at random and print them, the loop iterates 10 times then stops. As I understand it the only way to count the number of repetitions of some words is to place the words in an array or a split variable string and then run a pre-written cnt. Click Here to see that discussion.

I need to know how you get Python to take the random value it produced and then save it into an array according to the number of iterations of the for loop(in this case x number of iterations).

Here is the variable name and the two options:

coin = ["Heads", "Tails"]

Here is the code for the coin flipper core:

#Flipping core :)
def flipit(random, flip, time, comment, repeat):
    time.sleep(1)
    print("It begins...")
    print("\n")
    for x in range(0, 10):
        print("Flip number", x + 1)
        print(random.choice(comment))
        time.sleep(1)
        print(random.choice(coin),"\n")
        time.sleep(2)
        print("\n")
    from collections import Counter
    counting = []
    cnt = Counter(counting)
    cnt
    print("Type startup(time) to begin flipping coins again")

If you do feel like refining the code please do if you have the time, but all I need is a method that I can put into the overall program that will make it run properly.

Please don't worry about the random comment, that was for a bit of fun.

I have pasted the whole program on PasteBin, Click Here for that.

Thank you for reading this and my gratitude to those who respond or even fix it.

Edit: Just for reference I am a bit of a newbie to Python, I know some things but not even half of what the people who answer this will know.

Solution: I have managed to make Python "read" the random value using a per-iteration if statement in my for loop, using if statements I have added 1 to the respective variable according to the random.choice.

Here is the flip core code:

def flipit(random, time, comment, headcount, tailcount, side):
    time.sleep(1)
    print("It begins...")
    print("\n")
    for x in range(0, 10):
        print("Flip number", x + 1)
        side = random.choice(coin) # get the random choice
        print(random.choice(comment))
        time.sleep(1)
        print(side) # print it
        if side == "Heads":
            headcount += 1
        else:
            tailcount += 1
        time.sleep(2)
        print("\n")
    print("You got", headcount, "heads and", tailcount, "tails!")
    print("Type start() to begin flipping coins again")
    resetheadtail()

resetheadtail() is the new function I have added to reset the variables at the end of the program running.

For the full code click Here!

Thanks all who helped, and those who persevered with my newbieness :)

#comment necessary for edit, please ignore
Community
  • 1
  • 1
Freddie
  • 3
  • 3

1 Answers1

0

I think what you want to do is:

flip = random.choice(coin) # get the random choice
print(flip) # print it
counting.append(flip) # add it to the list to keep track

Note that you will need to move counting = [] to before your for loop.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • What if I were to produce the random values before, save them in an array by number of iterations then print them out from that array with the iterations? – Freddie Jan 17 '14 at 17:52
  • 1
    I'd say try it and see what happens..:) – Totem Jan 17 '14 at 17:53
  • I agree with @Totem. To help you on your way, see what happens when you `print(list(enumerate(["H", "T", "H"], 1)))`. – jonrsharpe Jan 17 '14 at 17:56
  • Sorry I am a bit of a newbie to Python, should have mentioned it in the question itself, where would I put these pieces of code? – Freddie Jan 17 '14 at 18:05
  • Use the three lines in block code in my answer to replace `print(random.choice(coin),"\n")`, with `counting = []` before `print("It begins...")`. – jonrsharpe Jan 17 '14 at 18:07
  • @jonrsharpe after inserting this into the code where you said how to I have the results shown? Is there some way to use the Counter() module for this? – Freddie Jan 17 '14 at 18:28
  • I have already given you one suggestion. And yes, you can use `Counter`; try reading [the documentation](http://docs.python.org/2/library/collections.html#counter-objects). – jonrsharpe Jan 17 '14 at 18:51