[Tutor] (no subject)

bhaaluu bhaaluu at gmail.com
Wed Oct 1 14:57:25 CEST 2008


Since an answer has already been given (by Pierre Dagenais),
let's see if we can help you "figure it out".

The first step is to read the program specification, or, in your
case, read the homework problem very carefully. Since most
computer programs have INPUT, PROCESS, and OUTPUT, let's
try to look at the problem from that viewpoint, shall we?

On Tue, Sep 30, 2008 at 10:47 PM, kayla bishop
<horses_kayla10 at hotmail.com> wrote:
> I can't figure out how to write a program where you flip a coin 100 times
> and it keeps track of how many heads and tails you flipped but it has to be
> random. Can you please help

Well, since you said the magic word, I'll try.

First of all, a hash mark designates a comment.
A comment isn't read by the computer. Everything
after a hash mark isn't read by the computer, BUT
you can read it, and that's why it is always a good
idea to use comments in your program.

We can break down the problem into INPUT, PROCESS
and OUTPUT as follows:

> I can't figure out how to [INPUT, PROCESS, OUTPUT]
> write a program
> where you flip a "coin" [INPUT]
> 100 times [PROCESS]
> and it keeps track of how many heads and tails you flipped [OUTPUT]
> but it has to be random. [PROCESS]

You have a coin which has two sides "heads" and "tails",
so that is the INPUT.
You flip the coin 100 times, and each time you flip it, the result is random.
That is the PROCESS.
You keep track of how many times it comes up heads and tails.
That is the OUTPUT.
That wasn't too difficult, was it?

Python has a lot of modules that are already written
for you. That's way cool because you don't have to
figure that part out! Doing random things is one of
those things you don't have to figure out, because
there is a module called random. You can use the
stuff in random by importing it into your program.
Find out more about random in the Python documentation.
This is usually done at the top of the program.

#!/usr/bin/python
# flipCoin.py
# 2008-10-01
# b h a a l u u at g m a i l dot c o m

import random

#Next you need some INPUT. I think we decided that the
#coin would be INPUT, right? And the coin has 'heads' and 'tails'?
#Let's make a "list" for the coin:

coin = ['heads', 'tails']

#Lists are surrounded by square brackets.
#But the program needs to choose those randomly, right?

flip = random.choice(coin)

#Now, let's count how many times you flip the coin.

count = 100

#Finally, keep track of heads and tails:

heads = 0
tails = 0

#They're zero because you haven't flipped the coin yet.

#Now, let's flip the coin (PROCESS).

while count != 0:
    #each time through the loop, flip will randomly choose a side of the coin
    flip = random.choice(coin)
    #if/else selection. There are only two choices.
    if flip == "heads":
        heads += 1
    else:
        tails += 1
    #decrement the counter, or you'll be in an infinite loop
    #it started at 100, so subtract one each iteration
    count -= 1

#Finally, let's print the OUTPUT:
print "Heads: ", heads
print "Tails: ", tails

Not the indentation after the while loop line.
That's a Python thing.

Since I've put so much time into this tutorial, I have a
challenge for you: Write a program that rolls a pair of
ten-sided dice, and tell me what the outcome of the roll is?
How about the outcome of three rolls?

Happy Programming!
-- 
b h a a l u u at g m a i l dot c o m
Kid on Bus: What are you gonna do today, Napoleon?
Napoleon Dynamite: Whatever I feel like I wanna do. Gosh!


More information about the Tutor mailing list