[Tutor] Issue w/ program: Flip a coin and count heads and tails

Asokan Pichai pasokan at talentsprint.com
Fri May 24 14:55:51 CEST 2013


On Fri, May 24, 2013 at 4:54 PM, Rafael Knuth <rafael.knuth at gmail.com>wrote:

>  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. It obviously does something else
>> than I intended, and I am wondering what I did wrong:
>>
>
> What is that you intended?
> Please explain that.
>
> I want that program to flip a coin 10 x times and then count the number of
> heads and tails.
> That's very much it.
>

Then you have set up the while loop right. It loops 10 times.
But if it is a head you are counting heads. Now if it is a tail
you are discarding and tossing again!

That is the effect of
while flips < 10:
    flips = flips + 1
    if random.randint(1,2) == 1:
#### - if it is not head?
        heads = heads + 1
        print("We've got " + str(heads) + " heads here."
    if random.randint(1,2) == 2:
#### - this is the second toss I am talking about
        tails = tails + 1

Now if you replace the second toss by an "else" and count it as a tail
you should go towards the code you are looking for. Try it and tell us
how it goes. All the best

Asokan Pichai
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130524/7491078c/attachment-0001.html>


More information about the Tutor mailing list