python random.randint函数不工作

nkhmeac6  于 2022-11-21  发布在  Python
关注(0)|答案(6)|浏览(179)

这个游戏的要点是将硬币抛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')
5vf7fwbs

5vf7fwbs1#

请尝试以下操作:

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:

gv8xihay

gv8xihay2#

打印方式不明确。您可以执行以下操作:

>>> 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
xesrikrc

xesrikrc3#

您的问题在这里:

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)
ltskdhd1

ltskdhd14#

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

试试这个

zqry0prt

zqry0prt5#

我知道这是几年前的事了,但我修正了连载的代码,以计算结尾的正面和反面的总数:

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}
''')
isr3a4wc

isr3a4wc6#

你把random放在循环外🙆,它只执行一次。

相关问题