Python code for the coin toss issues

2019-06-18 21:53发布

问题:

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?

回答1:

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


回答2:

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)


回答3:

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.



回答4:

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)


回答5:

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


回答6:

tosses = 100
heads = sum(random.randint(0, 1) for toss in range(tosses))
tails = tosses - heads


回答7:

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


回答8:

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


回答9:

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


回答10:

>>> import numpy as np
>>> coins = np.random.randint(2, size=100)
>>> print('Heads: ', np.sum(coins))
('Heads: ', 44)
>>> print('Tails: ', 100 - np.sum(coins))
('Tails: ', 56)
>>> print(['H' if i==1 else 'T' for i in coins])
['T', 'T', 'T', 'T', 'T', 'H', 'T', 'T', 'H', 'H', 'H', 'T', 'T', 'H', 'H', 'T', 'T', 'H', 'H', 'H', 'T', 'T', 'H', 'H', 'H', 'H', 'H', 'H', 'T', 'H', 'H', 'T', 'T', 'T', 'T', 'T', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'T', 'T', 'T', 'T', 'T', 'H', 'T', 'H', 'H', 'H', 'T', 'H', 'T', 'H', 'T', 'H', 'T', 'T', 'T', 'H', 'H', 'T', 'T', 'H', 'T', 'T', 'H', 'H', 'T', 'T', 'T', 'T', 'H', 'T', 'H', 'H', 'H', 'T', 'H', 'T', 'H', 'T', 'H', 'T', 'T', 'T', 'T', 'T', 'T', 'H', 'H', 'T', 'T', 'T', 'H', 'H', 'T']