Search code examples
pythonpython-2.7coin-flipping

Modified coin flip program in Python, can't work out the loop


So I'm required to write a lab for my Python class to flip a coin. Yes, it's been asked before, but this one in particular, I haven't seen any examples in any of the searches I've done. The program is supposed to accept input from the user, how many times to flip the coin. It then uses that input to flip the coin said number of times and record how many heads and tails. At the end, it will print the number of flips, and how many heads and tails. The program is then supposed to prompt the user to enter another number of flips, but with my program, each time, it accepts the second input, but skips the loop and ends the program. Also, an input of 0 is supposed to terminate the program. Here are the two examples I've tried:

import random

timesToFlip = input("Enter the number of times to flip the coin: ")

def coinFlipGame(timesToFlip):
    coinHeads = 0
    coinTails = 0
    accumulator = 0
    while timesToFlip > 0 and accumulator < timesToFlip:
        coinFlip = random.randint(0,1)
        if coinFlip == 1:
            accumulator += 1
            print "After", accumulator, "flip(s) of the coin, the result was heads!"
            coinHeads += 1
            raw_input("Press [ENTER] to continue...."); print
        else:
            accumulator += 1
            print "After", accumulator, "flip(s) of the coin, the result was tails!"
            coinTails +=1
            raw_input("Press [ENTER] to continue...."); print
    print "Heads =", coinHeads, "| Tails =", coinTails; print
    if timesToFlip == 0:
        print; print "You have chosen to end the game. Goodbye!"
    timesToFlip = input("Enter the number of times to flip the coin: ")

coinFlipGame(timesToFlip); print

And here's the other version:

import random

timesToFlip = input("Enter the number of times to flip the coin: ")

def coinFlipGame(timesToFlip):
    coinHeads = 0
    coinTails = 0
    accumulator = 0
    if timesToFlip == 0:
        print "You have chosen to end the game. Goodbye!"
    else:
        while timesToFlip > 0 and accumulator < timesToFlip:
            coinFlip = random.randint(0,1)
            if coinFlip == 1:
                accumulator += 1
                coinHeads += 1
                print accumulator, "coin flip(s) performed. Heads."
            else:
                    accumulator += 1
                coinTails += 1
                print accumulator, "coin flip(s) performed. Tails."
        print "Flips:", accumulator, "| Heads:", coinHeads, "| Tails:", coinTails
        timesToFlip = input("Enter the number of times to flip the coin: ")

coinFlipGame(timesToFlip)

Any help on how to get the input within the module to repeat the loop would be greatly appreciated! :D And yes, we have to use modules in the program, according to the professor.


Solution

  • Its not skipping the loop, your second input is outside the loop. maybe instead do this:

    def coinFlipGame(timesToFlip):
        coinHeads = 0
        coinTails = 0
        accumulator = 0
        while accumulator < timeToFlip:
            coinFlip = random.randint(0,1)
            if coinFlip == 1:
                accumulator += 1
                coinHeads += 1
                print accumulator, "coin flip performed. Heads."
            else:
                accumulator += 1
                coinTails += 1
                print accumulator, "coin flip performed. Tails."
        print "Flips:", accumulator, "| Heads:", coinHeads, "| Tails:", coinTails
    
    
    timesToFlip = int(input("Enter the number of times to flip the coin: "))
    while timesToFlip:
        coinFlipGame(timesToFlip)
        timesToFlip = int(input("Enter the number of times to flip the coin: "))
    

    Note: you are probably going to want to int() the input