[Tutor] complete neophyte question here

Lie Ryan lie.1296 at gmail.com
Mon Oct 12 16:32:13 CEST 2009

Jason Willis wrote:
> I've recently been going through "Python Programming, for the Absolute
> Beginner"  Second Edition, By Michael Dawson
> I'm running python 2.5.4 on a windows xp home machine.
> So my question boils down to this:
> At the end of one of his chapters there is a challenge to write a
> program that flips a "coin" 100 times then outputs the number of heads
> or tails it hit.
> I wrote this:

Let's break the program down:

> import random

on this line you're importing an external module. In python (and in
practically all languages), the random module is used to generate random
numbers. In practically all languages, there are mechanisms to use codes
written in another file, probably written by someone else. random is one
such external code. In python, this external code is called a module,
and the import mechanism allows you to use whatever code is in the
random module. The random module is part of python's standard library
and comes together when you installed python. Later, you will learn how
to create your own modules and how to import modules you've written
yourself. But how precisely the import mechanism works is not really
important right now.

> flip = 0
> head = 0
> tails = 0

Here you created some variables and assigned values to them;
specifically 0s.

> while flip != 100:

while-loop will repeat the block of code below it until "flip != 100"
evaluates to false. "flip != 100" evaluates to false when the value of
flip is 100.

>     flip += 1

the flip is your loop counter; you increment (add by one) every time the
block is run.

>     coin = random.randrange(2)

you call a function in the random module called randrange(). A function
is a piece of code that is meant for reuse; you can write your own
function like this:

def add(argument1, argument2):
    sum = argument1 + argument2
    return sum

and later in your code you could call add(3, 2) to get 5.

random.randrange() is one such functions that is defined by the random
module. randrange(n) returns a random number between 0 and n-1 (i.e. in
your case between 0 and 1). There are various advantages on returning
between 0 and n-1 instead of between 0 and n, but the justifications are
not important now.

>     if coin == 0
>         head += 1
>     elif coin == 1
>         tails += 1

That code does the main work that increment head by 1 when when coin is
0 or tails when coin is 1.

> print head, tails

and finally you printed the result to the screen.

> raw_input("\n<return>")
> I honestly don't know how code works and would appreciate someone
> explaining it to me?
> Using logic, I shouldn't be able to write the program due to my limited
> skills so  i  don't know how to explain HOW i wrote it because it just
> sort of happened. I was sitting home one night and it just came out
> itself.  I know that's not much of an explanation but there it is.

Why the pessimism? You have written a correct code (except for a few
minor syntactical issue). Congratulations for your first program.

More information about the Tutor mailing list