简体   繁体   English

python,重复random.randint吗?

[英]python, repeat random.randint?

I am new to python and i would like to know how to make the code to repeat the random.randint part 100 times. 我是python的新手,我想知道如何制作代码来将random.randint部分重复100次。

#head's or tail's

print("you filp a coin it lands on...")

import random

heads = 0
tails = 0


head_tail =random.randint(1, 2,)

if head_tail == 1:
    print("\nThe coin landed on heads")
else:
    print("\nThe coin landed on tails")

if head_tail == 1:
    heads += 1
else:
   tails += 1

flip = 0
while True :
    flip +=1
    if flip > 100:
        break



print("""\nThe coin has been fliped 100 times
it landed on heads""", heads, """times and tails""", tails,
"""times""")

input("Press the enter key to exit")

You could do it all in one line with a list comprehension: 您可以使用列表理解功能在一行中完成所有操作:

flips = [random.randint(1, 2) for i in range(100)]

And count the number of heads/tails like this: 并像这样计算头/尾的数量:

heads = flips.count(1)
tails = flips.count(2)

Or better yet: 或者更好:

num_flips = 100
flips = [random.randint(0, 1) for _ in xrange(num_flips)]
heads = sum(flips)
tails = num_flips - heads

First of all, I would replace that while loop with: 首先,我将while循环替换为:

for flip in xrange(100):
  ...

Secondly, to conduct 100 random trials, move the randint() call -- as well as everything else that you want to perform 100 times -- inside the body of the loop: 其次,要进行100次随机试验,请将randint()调用以及要执行100次的所有其他操作randint()循环体内:

for flip in xrange(100):
  head_tail = random.randint(1, 2)
  ...

Finally, here is how I would do the whole thing: 最后,这是将要做的全部事情:

heads = sum(random.randint(0, 1) for flip in xrange(100))
tails = 100 - heads

You would use range(100) , since you're on Python3.x which creates a list from 0 to 99 (100 items). 您将使用range(100) ,因为您使用的是Python3.x,它将创建一个从0到99(100个项目)的列表。 It'll look something like this: 它看起来像这样:

print("you flip a coin it lands on...")

import random

heads = 0
tails = 0


for i in xrange(100):
    head_tail = random.randint((1, 2))

    if head_tail == 1:
        print("\nThe coin landed on heads")
    else:
        print("\nThe coin landed on tails")

    if head_tail == 1:
        heads += 1
    else:
        tails += 1    


print("""\nThe coin has been fliped 100 times
it landed on heads""", heads, """times and tails""", tails,
"""times""")

input("Press the enter key to exit")
for flip in range(100):
    head_tail = random.randint(1, 2)

    if head_tail == 1:
        print("\nThe coin landed on heads")
    else:
        print("\nThe coin landed on tails")

    if head_tail == 1:
        heads += 1
    else:
        tails += 1

I'm new in Python and generally in programming, but I created my own code base on 1 day programming practice. 我是Python的新手,而且通常是编程人员,但是我是根据1天的编程实践创建了自己的代码库。 Coin flip coding exercise is one of the first from the book I study now. 硬币翻转编码练习是我现在学习的这本书的第一批练习之一。 I tried to find solution on the internet and I found this topic. 我试图在Internet上找到解决方案,然后找到了这个主题。

I was aware to use any code from this topic because few functions used in previous answers were not familiar to me. 我知道可以使用本主题中的任何代码,因为以前的答案中使用的功能很少我不熟悉。

To anyone in similar situation: Please feel free to use my code. 对于处于类似情况的任何人:请随时使用我的代码。

import random

attempts_no = 0
heads = 0
tails = 0

input("Tap any key to flip the coin 100 times")


while attempts_no != 100:
 number = random.randint(1, 2)
 attempts_no +=1
 print(number)
 if number == 1:
    heads +=1
 elif number ==2:
    tails +=1

print("The coin landed on heads", heads, "times")
print("The coin landed on heads", tails, "times")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM