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

Todd Matsumoto c.t.matsumoto at gmail.com
Sun May 26 10:57:11 CEST 2013


May I suggest running randint using 0, 1. The results can be tested like a
boolean. See what happens when you do an if test on 1 versus an if test on
0.

Also perhaps store your results in a dictionary with 'heads', 'tails' each
value set to 0.


On Sat, May 25, 2013 at 3:13 PM, Steven D'Aprano <steve at pearwood.info>wrote:

> On 25/05/13 19:25, Rafael Knuth wrote:
>
>  flips = 0
>> heads = 0
>> tails = 0
>>
>> while flips < 10:
>>      flips = flips + 1
>>
>
> Don't do this. It's not 1971 any more, we've had for loops for forty years
> :-)
>
> Use while loops when you don't know how many loops you need. When you know
> how many loops you will have, use a for loop.
>
> for flip in range(10):
>     print(flip)
>
> will print 0 1 2 3 ... 9 which makes ten loops altogether. If you want to
> start counting at 1 instead of 0, use:
>
> for flip in range(1, 11):
>     print(flip)
>
> which will print 1 2 3 ... 9 10.
>
>
> There's no need to count the number of heads and tails separately, since
> every loop is guaranteed to give either head or tails. So just count the
> number of heads, then the number of tails will be ten less the number of
> heads.
>
>
>
>
> --
> Steven
>
> ______________________________**_________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>



-- 
Todd Matsumoto
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130526/957b0f23/attachment-0001.html>


More information about the Tutor mailing list