[Tutor] Flip the coin 10x and count heads and tails: It works now!

Rafael Knuth rafael.knuth at gmail.com
Sat May 25 11:25:13 CEST 2013


Gents,

thank you all for your help. One of you guys asked me to try out your
suggestions and then tell you how it goes. Here we go! First, let me
recap briefly what the expected outcome of my program was and which
difficulties I encountered at the beginning. I was writing a program
in Python 3.3.0 which flips a coin 10 x times and then counts the
number of heads and tails. It obviously did something else than I
intended:

import random

print ("""

This program flips a coin 10 times.
It then counts the number of heads and tails.

""")

flips = 0
heads = 0
tails = 0

while flips < 10:
    flips = flips + 1
    if random.randint(1,2) == 1:
        heads = heads + 1
        print("We've got " + str(heads) + " heads here."
    if random.randint(1,2) == 2:
        tails = tails + 1
        print("We've got " + str(tails) + " tails here.")

This is what I got as output:

This program flips a coin 10 times.
It then counts the number of heads and tails.

We've got 1 tails here.
We've got 1 heads here.
We've got 2 tails here.
We've got 2 heads here.
We've got 3 tails here.
We've got 3 heads here.
We've got 4 tails here.
We've got 5 tails here.
We've got 4 heads here.
We've got 6 tails here.
We've got 7 tails here.
We've got 5 heads here.

As it turned out, each of your answers was *partially* correct, so I
played around with your suggestions until I got my code up and
running. Frankly, I did two mistakes: First, I placed the print
statement inside the loop instead of outside of it. And second, I
mistakenly called the random function two times inside the loop. In
order to resolve that issue, I had to modify the heads and tails
variables and then add another variable and put it inside the loop:

result = random.randint(1,2)

Lastly, I figured out that counting the results can be done in a more
elegant fashion. Instead of:

variable_XYZ = variable_XYZ + 1

… it can be done this way:

variable_XYZ += 1

I got so excited when I saw my program run properly that I decided to
pimp it a little bit by printing each head and tail with a time delay
while the coin got flipped during the run. Here’s what my code looks
like now:

import random
import time

print ("""

This program flips a coin 10 times.
It then counts the number of heads and tails.

""")
time.sleep(1)

flips = 0
heads = 0
tails = 0

while flips < 10:
    result = random.randint(1,2)
    if result == 1:
        heads += 1
        print("head")
        time.sleep(1)
    elif result == 2:
        tails += 1
        print("tail")
        time.sleep(1)
    flips += 1
print ("\nYou've got " + str(heads) + " heads and " + str(tails) + " tails.")

I have one last question to you: I explained twice in my initial mail
what I expect my program to do (“I am writing a program in Python
3.3.0 which flips a coin 10 x times and then counts the number of
heads and tails.”). I did that the introduction as well as inside my
code. However, almost each of you asked me what I expect my program to
do. I was confused about that and I am wondering if any of you can
clarify? I just want to make sure I avoid misunderstandings like these
in the future.

Thank you all again!

All the best,



Rafael


More information about the Tutor mailing list