0

我正在尝试编写一个程序来创建一个模拟掷硬币的游戏。我想问用户应该扔多少次硬币。根据响应,程序应该为指定的次数选择 0(正面)或 1(反面)的数字。我希望它打印如下内容:“[heads, tails, 'heads', 'heads']: there were 3 head and 2 tails”

到目前为止,这是我的代码:

import random
#this function creates a game that simulates a coin
#I will ask how many times that the coin should be tossed
def coinToss():
    number = input("How many times do you want to flip the coin?: ")
    myList = []
    counter = 0
        for element in range(number):
            flip = random.randint(0, 1)
            if flip == 0:
                myList.append("Heads")
            else:
                myList.append("Tails")


print(str(myList)) + str(":there were ") + str(counter) + str(" Heads ") + str(" and ") + str(counter) + str(" Tails")

我有我的可变计数器,但我如何将正面和反面相加?很困惑。对不起,如果这对你来说听起来很容易。我知道我最后的打印会出错。

4

3 回答 3

3

而不是附加"heads"or "tails"to myList,附加数字:

for element in range(number):
    flip = random.randint(0, 1)
    myList.append(flip)

现在,当您转到 print 语句时,您可以使用sum()and len()

print "There were {} heads and {} tails".format(len(myList) - sum(myList), sum(myList))

由于您的列表将包含类似[0, 1, 1, 0, 1, 0, 0](示例)的内容,因此将长度减去总和将为您提供零(正面)的数量,而获得总和将为您提供一的数量(尾部)。

使用字符串格式更整洁:)

于 2013-09-16T22:26:59.073 回答
0

这应该适合你

myList.count("Heads")
于 2013-09-16T22:26:33.933 回答
0
import random

def coinToss():
    numToss = input("How many times do you want to flip the coin?: ")
    tossList = []
        for element in range(number):
            flip = random.randint(0, 1)
            if flip == 0:
                tossList.append("Heads")
            else:
                tossList.append("Tails")
    print tossList, ": there were %d heads and %d tails" %(tossList.count("Heads"), tossList.count("Tails"))

或者

def coinToss():
    numToss = input("How many times do you want to flip the coin?: ")
    tossList = []
    headCounter = 0
        for element in range(number):
            flip = random.randint(0, 1)
            if flip == 0:
                tossList.append("Heads")
                headCounter += 1
            else:
                tossList.append("Tails")
    print tossList, ": there were %d heads and %d tails" %(headcounter, len(tossList)-headCounter)

或者

def coinToss():
    numToss = input("How many times do you want to flip the coin?: ")
    tossList = []
        for element in range(number):
            tossList.append(random.randint(0, 1))
    print tossList, ": there were %d heads and %d tails" %(tossList.count(1), tossList.count("Tails"))

或者

def coinToss():
    numToss = input("How many times do you want to flip the coin?: ")
    tossList = []
        for element in range(number):
            tossList.append(random.randint(0, 1))
    print tossList, ": there were %d heads and %d tails" %(sum(tossList), tossList.count("Tails"))

或者

def coinToss():
    numToss = input("How many times do you want to flip the coin?: ")
    tossList = [random.randint(0, 1) for _ in xrange(numToss)]
    print tossList, ": there were %d heads and %d tails" %(sum(tossList), tossList.count("Tails"))

或者

def coinToss():
    tossList = [random.randint(0, 1) for _ in xrange(input("How many times do you want to flip the coin?: "))]
    print tossList, ": there were %d heads and %d tails" %(sum(tossList), tossList.count("Tails"))
于 2013-09-16T22:33:13.103 回答