python - infinite coin flip that stops when number of heads = number of tails

Mei Lian Hoe

I'm new to python and I'm trying to create a coinflip loop which will keep flipping and counting the number of flips until the number of heads = the number of tails, where it will stop and print the total number of flips it took to reach that. I'm trying to get the results in order to work on my maths coursework, but I cannot seem to figure out how to get it to stop or print the results, and when I do it prints 0. Here is the code I have so far:

import random
heads = 1
tails = sum(random.choice(['head', 'tail']) == 'tail'
count = 0
while True:
    coinresult = random.randint(1, 2) if heads == tails:
    break

print("The number of flips was {count}".format(count = heads + tails))
James Kent

not sure what is going on with your indentation but try this:

import random
heads = 0 #initialize the count variables
tails = 0

while True:
    coinresult = random.randint(1, 2) #flip coin
    if coinresult == 1: #if result = 1 then increment heads counter
        heads += 1
    elif coinresult == 2: #if result = 2 then increment tails counter
        tails += 1
    if heads == tails: #check if counts are equal and break loop if they are
        break

print("The number of flips was {count}".format(count = heads + tails))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

python - infinite coin flip that stops when number of heads = number of tails

From Dev

How flipping a coin and display tails and heads counting?

From Dev

Python Heads and Tails

From Dev

Javascript - Rigged coin flip (heads first)

From Dev

Coin flip simulation never exceeding a streak of 15 heads

From Dev

Control the number of arrow heads

From Dev

Interpretation of number of heads returned by fdisk

From Java

How can I represent an infinite number in Python?

From Dev

Infinite sequencing number generator not work in python?

From Java

Flipping a coin and stopping when it lands heads 4 times in a row

From Dev

Random Number Generator - That Stops

From Dev

Finding number of ways to make a sum in coin changing?

From Dev

Tallying the outcome of a coin flip

From Dev

Coin flip command

From Dev

ViewPager for potentially infinite number of pages

From Dev

Infinite while loop with a random number

From Dev

Django urlpattern with infinite number of parameters

From Dev

Infinite while loop with a random number

From Dev

Return true if a binary number either contains all zero or all ones on a single flip.(Python)

From Dev

Application Stops while getting IMEI Number

From Dev

Spaces Control+Number stops working on Yosemite

From Dev

Match number in tab-stops only

From Dev

Setting two increments in a loop that stops at a certain number

From Dev

Flip coin simulation with R programming

From Dev

Coin flip program on c++

From Dev

Coin Flip HTML / Javascript | (Help)

From Dev

What defines when truncation should occur on an infinite binary number (0.1) to represent it in a scientific notation

From Dev

What defines when truncation should occur on an infinite binary number (0.1) to represent it in a scientific notation

From Dev

CSS sequential animation cascade for infinite number of elements

Related Related

  1. 1

    python - infinite coin flip that stops when number of heads = number of tails

  2. 2

    How flipping a coin and display tails and heads counting?

  3. 3

    Python Heads and Tails

  4. 4

    Javascript - Rigged coin flip (heads first)

  5. 5

    Coin flip simulation never exceeding a streak of 15 heads

  6. 6

    Control the number of arrow heads

  7. 7

    Interpretation of number of heads returned by fdisk

  8. 8

    How can I represent an infinite number in Python?

  9. 9

    Infinite sequencing number generator not work in python?

  10. 10

    Flipping a coin and stopping when it lands heads 4 times in a row

  11. 11

    Random Number Generator - That Stops

  12. 12

    Finding number of ways to make a sum in coin changing?

  13. 13

    Tallying the outcome of a coin flip

  14. 14

    Coin flip command

  15. 15

    ViewPager for potentially infinite number of pages

  16. 16

    Infinite while loop with a random number

  17. 17

    Django urlpattern with infinite number of parameters

  18. 18

    Infinite while loop with a random number

  19. 19

    Return true if a binary number either contains all zero or all ones on a single flip.(Python)

  20. 20

    Application Stops while getting IMEI Number

  21. 21

    Spaces Control+Number stops working on Yosemite

  22. 22

    Match number in tab-stops only

  23. 23

    Setting two increments in a loop that stops at a certain number

  24. 24

    Flip coin simulation with R programming

  25. 25

    Coin flip program on c++

  26. 26

    Coin Flip HTML / Javascript | (Help)

  27. 27

    What defines when truncation should occur on an infinite binary number (0.1) to represent it in a scientific notation

  28. 28

    What defines when truncation should occur on an infinite binary number (0.1) to represent it in a scientific notation

  29. 29

    CSS sequential animation cascade for infinite number of elements

HotTag

Archive