硬币折腾问题的Python代码

时间:2011-06-26 21:31:38

标签: python random statistics coin-flipping

我一直在用python编写一个程序来模拟100次掷硬币并给出投掷总数。问题是我还要打印头尾的总数。

这是我的代码:

import random
tries = 0
while tries < 100:
    tries += 1
    coin = random.randint(1, 2)
    if coin == 1:
        print('Heads')
    if coin == 2:
        print ('Tails')
total = tries
print(total)

我一直在绞尽脑汁寻求解决方案,到目前为止我一无所有。除了投掷总数之外,有没有办法获得打印的头尾数?

12 个答案:

答案 0 :(得分:14)

import random

samples = [ random.randint(1, 2) for i in range(100) ]
heads = samples.count(1)
tails = samples.count(2)

for s in samples:
    msg = 'Heads' if s==1 else 'Tails'
    print msg

print "Heads count=%d, Tails count=%d" % (heads, tails)

答案 1 :(得分:4)

你有一个尝试次数的变量,它允许你在最后打印,所以只需使用相同的方法来计算头尾数。在循环外创建headstails变量,在相关if coin == X块内增量,然后在结尾打印结果。

答案 2 :(得分:3)

import random

total_heads = 0
total_tails = 0
count = 0


while count < 100:

    coin = random.randint(1, 2)

    if coin == 1:
        print("Heads!\n")
        total_heads += 1
        count += 1

    elif coin == 2:
        print("Tails!\n")
        total_tails += 1
        count += 1

print("\nOkay, you flipped heads", total_heads, "times ")
print("\nand you flipped tails", total_tails, "times ")

答案 3 :(得分:1)

保持头部数量的运行轨迹:

import random
tries = 0
heads = 0
while tries < 100:
    tries += 1
    coin = random.randint(1, 2)
    if coin == 1:
        heads += 1
        print('Heads')
    if coin == 2:
        print ('Tails')
total = tries
print('Total heads '.format(heads))
print('Total tails '.format(tries - heads))
print(total)

答案 4 :(得分:1)

import random
tries = 0
heads=0
tails=0
while tries < 100:
    tries += 1
    coin = random.randint(1, 2)
    if coin == 1:
        print('Heads')
        heads+=1
    if coin == 2:
        print ('Tails')
        tails+=1
total = tries
print(total)
print tails
print heads

答案 5 :(得分:1)

# Please make sure to import random.

import random

# Create a list to store the results of the for loop; number of tosses are limited by range() and the returned values are limited by random.choice().

tossed = [random.choice(["heads", "tails"]) for toss in range(100)]

# Use .count() and .format() to calculate and substitutes the values in your output string.

print("There are {} heads and {} tails.".format(tossed.count("heads"), tossed.count("tails")))

答案 6 :(得分:1)

tosses = 100
heads = sum(random.randint(0, 1) for toss in range(tosses))
tails = tosses - heads

答案 7 :(得分:1)

您可以使用random.getrandbits()一次生成所有100个随机位:

import random

N = 100
# get N random bits; convert them to binary string; pad with zeros if necessary
bits = "{1:>0{0}}".format(N, bin(random.getrandbits(N))[2:])
# print results
print('{total} {heads} {tails}'.format(
    total=len(bits), heads=bits.count('0'), tails=bits.count('1')))

Output

100 45 55

答案 8 :(得分:0)

我最终得到了这个。

import random

flips = 0
heads = 0
tails = 0

while flips < 100:
    flips += 1

    coin = random.randint(1, 2)
    if coin == 1:
       print("Heads")
       heads += 1

    else:
       print("Tails")
       tails += 1

total = flips

print(total, "total flips.")
print("With a total of,", heads, "heads and", tails, "tails.")

答案 9 :(得分:0)

>>> import numpy as np
>>> coins = np.random.randint(2, size=100)
>>> print('Heads: ', np.sum(coins))
('Heads: ', 44)
>>> print('Tails: ', 100 - np.sum(coins))
('Tails: ', 56)
>>> print(['H' if i==1 else 'T' for i in coins])
['T', 'T', 'T', 'T', 'T', 'H', 'T', 'T', 'H', 'H', 'H', 'T', 'T', 'H', 'H', 'T', 'T', 'H', 'H', 'H', 'T', 'T', 'H', 'H', 'H', 'H', 'H', 'H', 'T', 'H', 'H', 'T', 'T', 'T', 'T', 'T', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T', 'H', 'T', 'H', 'H', 'H', 'T', 'H', 'T', 'H', 'T', 'H', 'T', 'T', 'T', 'H', 'H', 'T', 'T', 'H', 'T', 'T', 'H', 'H', 'T', 'T', 'T', 'T', 'H', 'T', 'H', 'H', 'H', 'T', 'H', 'T', 'H', 'T', 'H', 'T', 'T', 'T', 'T', 'T', 'T', 'H', 'H', 'T', 'T', 'T', 'H', 'H', 'T']

答案 10 :(得分:0)

这是我的代码。希望对您有所帮助。

import random

coin = random.randint (1, 2)

tries = 0
heads = 0
tails = 0

while tries != 100:

if coin == 1:
    print ("Heads ")
    heads += 1
    tries += 1
    coin = random.randint(1, 2)

elif coin == 2:
    print ("Tails ")
    tails += 1
    tries += 1
    coin = random.randint(1, 2)
else:
    print ("WTF")

print ("Heads = ", heads)
print ("Tails = ", tails)

答案 11 :(得分:0)

import random

print("coin flip begins for 100 times")

tails = 0
heads = 0
count = 0
while count < 100: #to flip not more than 100 times
count += 1
result = random.randint(1,2) #result can only be 1 or 2.

    if result == 1: # result 1 is for heads
        print("heads")
    elif result == 2: # result 2 is for tails
        print("tails")
    if result == 1:
        heads +=1 #this is the heads counter.
    if result == 2:
        tails +=1 #this is the tails counter.
# with all 3 being the count, heads and tails counters,
# i can instruct the coin flip not to exceed 100 times, of the 100 flips 
# with heads and tails counter, 
# I now have data to display how of the flips are heads or tails out of 100.
print("completed 100 flips") #just to say 100 flips done.
print("total tails is", tails) #displayed from if result == 2..... tails +=1
print("total heads is", heads)