Search code examples
pythonarraysfor-looprandomfixed

How do you make python read a random value it printed and insert it into an array?


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

Solution

  • 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.