Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
修改了Python中的抛硬币程序,可以';I don’我不知道该怎么做_Python_Python 2.7_Coin Flipping - Fatal编程技术网

修改了Python中的抛硬币程序,可以';I don’我不知道该怎么做

修改了Python中的抛硬币程序,可以';I don’我不知道该怎么做,python,python-2.7,coin-flipping,Python,Python 2.7,Coin Flipping,因此,我需要为我的Python类编写一个实验室来抛硬币。是的,以前有人问过,但这一次,我在我做的任何搜索中都没有看到任何例子。程序应该接受用户的输入,即掷硬币的次数。然后,它使用该输入来翻转硬币的次数,并记录有多少个正面和反面。最后,它将打印翻转的次数以及正面和反面的数量。然后,程序应该提示用户输入另一个翻转次数,但对于我的程序,每次都接受第二个输入,但跳过循环并结束程序。此外,输入0应该会终止程序。以下是我尝试过的两个例子: import random timesToFlip = input

因此,我需要为我的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
随机导入
timesToFlip=input(“输入抛硬币的次数:”)
def coinFlipGame(timesToFlip):
硬币头=0
一致性=0
累加器=0
当timesToFlip>0且累加器
这是另一个版本:

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)
随机导入
timesToFlip=input(“输入抛硬币的次数:”)
def coinFlipGame(timesToFlip):
硬币头=0
一致性=0
累加器=0
如果timesToFlip==0:
打印“您已选择结束游戏。再见!”
其他:
当timesToFlip>0且累加器

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

这不是跳过循环,你的第二个输入在循环之外。或许可以这样做:

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: "))
def coinFlipGame(timesToFlip):
硬币头=0
一致性=0
累加器=0
当蓄能器

注意:您可能需要
int()
输入谢谢大家的帮助!下面是完整的代码: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
“”
_分子束外延_
CIS-115-09
实验6-1
使用模块/函数编写python程序来模拟翻转
使用随机数发生器的硬币。
如果随机数发生器返回一个0,考虑一个“尾部”,
和一个1 a“头”的回报。
在节目开始时,询问用户掷硬币的次数,
记录“正面”和“反面”的总数,然后打印结果
硬币已按要求的次数翻转。
允许用户输入另一个掷硬币次数
然后重新运行程序。
输入零(0)次抛硬币将终止程序。
'''
印刷品;printHeader=原始输入(“请输入您的姓名:”)
印刷品;打印打印标题“|实验6-1”;打印
原始输入(“按[ENTER]继续…”);打印
导入随机#导入随机库以允许使用随机
#随机函数
#声明变量以获取关于掷硬币次数的输入
timesToFlip=int(输入(“输入抛硬币的次数:”);打印
#定义抛硬币游戏的功能
def coinFlipGame(timesToFlip):
coinHeads=0#用于存储硬币正面的次数
coinTails=0#用于存储硬币的尾数
累加器=0#确保硬币只翻转一定次数
当蓄能器