抛硬币的Python代码

问题描述 投票:6回答:10

我一直在用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)

我一直在绞尽脑汁寻求解决方案,到目前为止我什么都没有。除了扔掉的总数之外,还有什么方法可以打印出正面和反面的数量?

python random statistics coin-flipping
10个回答
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 ")

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

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)

4
投票

您有一个用于尝试次数的变量,它允许您在末尾打印该变量,因此只需对头和尾的数目使用相同的方法即可。在循环外部创建一个headstails变量,在相关的if coin == X块内递增,然后在最后打印结果。


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)

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

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

1
投票

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

random.getrandbits()

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

1
投票
100 45 55

0
投票

我结束了这个。

# 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")))
© www.soinside.com 2019 - 2024. All rights reserved.