1

所以我需要为我的 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 是的,教授说,我们必须在程序中使用模块。

4

2 回答 2

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()输入

于 2013-10-15T18:50:36.610 回答
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
于 2013-10-22T22:13:50.090 回答