0

这个游戏的要点是掷硬币 100 次并记录正面或反面。在 100 次翻转结束时,程序应该打印出它翻转正面和反面的次数。该程序仅在我运行时打印头,我觉得它是通过执行第一个 elif 子句而不是 random.randint 函数来执行此操作的。

任何人都可以帮助完成这个程序吗?

import random

print ('Coin flip game')
start = input('Press enter to flip the coin')
coin_flip = random.randint(1, 2)
heads = int(1)
tails = int(2)
heads = int(heads)
tails - int(tails)
count = 0
while coin_flip:
    count += 1
    if count == 100:
        break

    elif coin_flip == 1:
        print ('Heads')

    elif coin_flip == 2:
        print ('Tails')
4

5 回答 5

1

您希望它如何打印尚不清楚。你可以这样做:

>>> import random
>>> print [['H', 'T'][random.randint(0, 1)] for _ in range(100)]
['T', 'T', 'T', 'H', 'T', 'T', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'H', 'T', 'H', 'H',
 'H', 'H', 'H', 'T', 'T', 'H', 'T', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'T', 'T',
 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H',
 'H', 'H', 'T', 'H', 'T', 'H', 'T', 'H', 'H', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'T',
 'H', 'H', 'T', 'T', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'H', 'T', 'T',
 'H', 'H', 'H', 'H', 'T', 'H', 'H', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'T',]

或者:

>>> heads = sum(random.randint(0, 1) for _ in range(100))
>>> print "heads =", heads, ' tails =', 100 - heads
heads = 46  tails = 54
于 2013-07-17T03:34:59.207 回答
1

尝试这样的事情:

import random

print("Welcome to the coin flipper!")

start = input('Press enter to flip the coin')
count = 0
while True:
    coin_flip = random.randint(1,2)
    count+=1

    if count >100:
        break

    if coin_flip == 1:
        print('Heads')

    elif coin_flip == 2:
        print("Tails")

这样循环的每次迭代,数字都会改变,因为它在循环内

你也不需要分配所有你以后甚至不使用的变量,你应该使用while True:循环

于 2013-07-17T03:12:55.940 回答
0

你的问题在这里:

while coin_flip: # coin_flip is set to 1 or 2
    ...

由于coin_flip设置为 1 或 2,您的循环将是无限的,重要的是它 coin_flip不会改变(您没有更新它)。

要解决此问题,您应该通过在循环coin_flip为其分配一个随机数来更新您的:

while True:
    count += 1
    coin_flip = random.randint(1, 2) # you need to "flip" coin inside the loop
    if count == 100:
        break

    elif coin_flip == 1:
        print ('Heads')

    elif coin_flip == 2:
        print ('Tails')

要计算磁头的数量,您可以使用一个headstails变量来跟踪:

heads = 0
tails = 0
while True:
    count += 1
    coin_flip = random.randint(1, 2) # you need to "flip" coin inside the loop
    if count == 100:
        break

    elif coin_flip == 1:
        print ('Heads')
        heads += 1

    elif coin_flip == 2:
        print ('Tails')
        tails += 1

print "Number of heads: %s, Number of tails: %s" % (heads, tails)
于 2013-07-17T03:37:44.003 回答
0
heads,tails,=0,0
import random
for i in range(100):
    a=random.randint(1,2)
        if(a%2==0):
            tails+=1
        else:
        heads+=1
print 'no. of heads in 100 flips,',heads
print 'no. of tails in 100 flips,',tails

试试这个

于 2017-09-10T17:17:27.750 回答
0

我知道这是几年前的事了,但我修复了 Serials 的代码,以便在最后计算正面和反面的总数:

import random

print("Welcome to the coin flipper!")

start = input('Press enter to flip the coin')
count = 0
heads = 0
tails = 0
while True:
    coin_flip = random.randint(1,2)
    count+=1

    if count >100:
        break

    if coin_flip == 1:
        print('Heads')
        heads += 1

    elif coin_flip == 2:
        print("Tails")
        tails += 1
print(f'''
Number of tails: {tails}
Number of times heads {heads}
''')
于 2021-01-19T02:38:49.390 回答