7

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

我一直在绞尽脑汁寻找解决方案,但到目前为止我一无所获。除了投掷的总数之外,还有什么方法可以打印出正面和反面的数量?

4

11 回答 11

16
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)
于 2011-06-26T21:35:50.763 回答
4

您有一个尝试次数的变量,它允许您在最后打印它,因此只需对正面和反面的数量使用相同的方法。在循环外创建一个heads和变量,在相关块内递增,然后在最后打印结果。tailsif coin == X

于 2011-06-26T21:33:24.877 回答
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 ")
于 2015-04-20T19:22:21.053 回答
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)
于 2011-06-26T21:36:01.917 回答
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
于 2011-06-26T21:43:30.600 回答
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")))
于 2012-01-27T22:59:28.710 回答
1
tosses = 100
heads = sum(random.randint(0, 1) for toss in range(tosses))
tails = tosses - heads
于 2012-01-27T23:39:22.610 回答
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')))

输出

100 45 55
于 2012-01-28T09:40:37.797 回答
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.")
于 2017-04-04T00:01:08.727 回答
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)
于 2019-12-25T15:04:45.893 回答
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)
于 2020-02-07T17:53:40.870 回答