質問

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.

役に立ちましたか?

解決

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

他のヒント

Thank you guys for all the help! Here's the completed code in all its glory :D

'''
_MBE_
CIS-115-09
Lab 6-1

Write a python program, using modules / functions, to simulate flipping
a coin using a random number generator. 
If the random number generator returns a 0 consider that a “tails”,
and a return of a 1 a “heads”. 
At the beginning of the program ask the user how many times to flip the coin,
keep track of the total “heads” and “tails” and print the results after
the coin has been flipped the requested number of times. 
Allow the user to enter another number of times to flip the coin
and re-run the program.
An input of zero (0) times to flip the coin will terminate the program.
'''

print; printHeader = raw_input("Please enter your name: ")

print; print printHeader, "| Lab 6-1"; print

raw_input("Press [ENTER] to continue...."); print

import random   # importing the random library to allow for use of random
                #random functions later on

# declaring the variable to take input on how many times to flip the coin
timesToFlip = int(input("Enter the number of times to flip the coin: ")); print

# defining the function for the coin flip game
def coinFlipGame(timesToFlip):
    coinHeads = 0       # used to store how many times the coin is heads
    coinTails = 0       # used to store how many times the coin is tails
    accumulator = 0     # ensures that the coin is only flipped a certain number of times
    while accumulator < timesToFlip:
        coinFlip = random.randint(0,1)
        if coinFlip == 1:
            accumulator += 1
            coinHeads += 1
            print "After", accumulator, "flip(s) of the coin, the result was heads!"
            raw_input("Press [ENTER] to continue...."); print
        else:
            accumulator += 1
            coinTails += 1
            print "After", accumulator, "flip(s) of the coin, the result was tails!"
            raw_input("Press [ENTER] to continue...."); print
    print "Flips:", accumulator, "| Heads:", coinHeads, "| Tails:", coinTails

while timesToFlip:      # a loop to allow the program to keep running until
                        #an appropriate kill code is entered
    coinFlipGame(timesToFlip)
    timesToFlip = input("Enter the number of times to flip the coin: "); print
    if timesToFlip == 0:
    print "You have ended the game. Goodbye!"; print

raw_input("Press [ENTER] to end program....")

# end program
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top