Jack Tapping
Jack Tapping

Reputation: 15

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.

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

Upvotes: 1

Views: 6463

Answers (5)

buba
buba

Reputation: 1

I'm new in Python and generally in programming, but I created my own code base on 1 day programming practice. 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.

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

Upvotes: 0

Joel Cornett
Joel Cornett

Reputation: 24788

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

Upvotes: 5

NPE
NPE

Reputation: 500237

First of all, I would replace that while loop with:

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:

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

Upvotes: 3

TankorSmash
TankorSmash

Reputation: 12747

You would use range(100), since you're on Python3.x which creates a list from 0 to 99 (100 items). 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")

Upvotes: 2

Junuxx
Junuxx

Reputation: 14251

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

Upvotes: 1

Related Questions