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