抛硬币问题的Python代码

发布于 2024-11-17 04:06:09 字数 374 浏览 3 评论 0原文

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

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

I've been writing a program in python that simulates 100 coin tosses and gives the total number of tosses. The problem is that I also want to print the total number of heads and tails.

Here's my code:

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)

I've been racking my brain for a solution and so far I have nothing. Is there any way to get the number of heads and tails printed in addition to the total number of tosses?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

茶色山野 2024-11-24 04:06:09
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)
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)
萌无敌 2024-11-24 04:06:09
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 ")
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 ")
巴黎夜雨 2024-11-24 04:06:09

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

You have a variable for the number of tries, which allows you to print that at the end, so just use the same approach for the number of heads and tails. Create a heads and tails variable outside the loop, increment inside the relevant if coin == X block, then print the results at the end.

深居我梦 2024-11-24 04:06:09

持续跟踪头部数量:

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)

Keep a running track of the number of heads:

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)
刘备忘录 2024-11-24 04:06:09
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
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
赠意 2024-11-24 04:06:09
tosses = 100
heads = sum(random.randint(0, 1) for toss in range(tosses))
tails = tosses - heads
tosses = 100
heads = sum(random.randint(0, 1) for toss in range(tosses))
tails = tosses - heads
转瞬即逝 2024-11-24 04:06:09

您可以使用 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

You could use random.getrandbits() to generate all 100 random bits at once:

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
半葬歌 2024-11-24 04:06:09
# 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")))
# 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")))
得不到的就毁灭 2024-11-24 04:06:09

我最终得到了这个。

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

I ended up with this.

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.")
小清晰的声音 2024-11-24 04:06:09

这是我的代码。希望它会有所帮助。

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)

Here is my code. Hope it will help.

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)
神妖 2024-11-24 04:06:09
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)
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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文