在Python中修改了硬币翻转程序,无法解决循环问题

时间:2013-10-15 18:44:12

标签: python python-2.7 coin-flipping

所以我需要为我的Python类编写一个实验室来翻转硬币。是的,之前有人问过,但特别是这个,我在任何搜索中都没有看到任何例子。该程序应该接受来自用户的输入,翻转硬币的次数。然后它使用该输入来翻转硬币所述次数并记录多少个头尾。最后,它将打印翻转的数量,以及有多少头和尾。然后该程序将提示用户输入另一个翻转次数,但是对于我的程序,每次都接受第二次输入,但跳过循环并结束程序。此外,输入0应该终止程序。以下是我尝试过的两个例子:

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

这是另一个版本:

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)

如何获取模块中的输入以重复循环的任何帮助将不胜感激! :D是的,根据教授的说法,我们必须在程序中使用模块。

2 个答案:

答案 0 :(得分:1)

它没有跳过循环,你的第二个输入是在循环之外。也许这样做:

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: "))

注意:您可能希望int()输入

答案 1 :(得分:0)

谢谢大家的帮助!这是完整代码的所有荣耀: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