[Tutor] Rock, Paper, Scissors

John Fouhy john at fouhy.net
Tue Aug 8 07:54:36 CEST 2006


On 08/08/06, Christopher Spears <cspears2002 at yahoo.com> wrote:
> This looks like a fun project to work on.  From
> reading the description, I feel this would be pretty
> straight forward game to program.  However, I have no
> idea how the computer would decide if it wanted a
> rock, paper, or a pair of scissors.  Any hints?

Well, the easiest way to do it would be to make it random --- have a
look at the random module in the standard library.

For example, random.randrange(3) will give you a random integer in [0,
1, 2], which you could map to rock/paper/scissors.

[the random module also has a function randint, which is similar to
randrange.  I prefer randrange, because the arguments match the
arguments to range(), whereas randint is a bit different]

Or random.choice(['rock', 'paper', 'scissors']) will give you a string.

Of course, if you want to be more clever, I understand that RSP
programs that always pick what the human picked in the previous round
tend to do better than random (unless the human notices, at which
point they start doing very badly :-) ).

-- 
John.


More information about the Tutor mailing list