Re: [Tutor] Rock, Paper, Scissors game

2013-07-26 Thread Alan Gauld

On 26/07/13 12:22, Saad Javed wrote:


WEAPONS = 'Rock', 'Paper', 'Scissors'



if cpu != player:
   if (player - cpu) % 3 < (cpu - player) % 3: #How does this line work?


Try it in  the interpreter...

the two subtractions will always result in +/-1 or +/-2

>>> -2 % 3
1
>>> 2 % 3
2

>>> -1 % 3
2
>>> 1 % 3
1

Now see how those work in the if statement.

It's not how I would implement it(*) but its neat enough
and works.

(*)I'd probably use a dictionary or look up table.

HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rock, Paper, Scissors game

2013-07-26 Thread Dave Angel

On 07/26/2013 07:22 AM, Saad Javed wrote:

I'm trying to understand how this code works. I've commented the line
that I can't seem to understand. How did the guy come up with this?

#!/usr/bin/env python

import random

#rock > scissor > paper > rock

WEAPONS = 'Rock', 'Paper', 'Scissors'

for i in range(0, 3):
   print "%d %s" % (i + 1, WEAPONS[i])

player = int(input ("Choose from 1-3: ")) - 1
cpu = random.choice(range(0, 3))

print "%s vs %s" % (WEAPONS[player], WEAPONS[cpu])
if cpu != player:
   if (player - cpu) % 3 < (cpu - player) % 3: #How does this line work?


I'm not sure what aspect of it you're puzzled about, but % is the modulo 
operator.  It produces the remainder upon integer division.  So we're 
effectively dividing by 3, and keeping only the remainder, which will be 
0, 1, or 2.


It will only be zero if cpu==player, but we've already tested that.  So 
the only two cases are 1 and 2.  A little experimenting will show you 
that if the first part( (player-cpu)%3 ) is 1, the second will be 2, and 
vice versa.


So the statement could be simplified to:

 if (player - cpu) % 3 == 1:

Now is it clear?



 print "Player wins"
   else:
 print "CPU wins"
else:
   print "tie!"



BTW, that input statement should be raw_input, since you're running it 
on Python 2.x.



--
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rock, Paper, Scissors

2006-08-12 Thread Jesus Rodriguez
Hi, i used a list with R, P, S like:rps = ["Rock", "Paper", "Scissors"]random.shuffle(rps)computerObject = rps[0]Then, the computer pick a random object and i compare my object with computer's object.
;)2006/8/11, wesley chun <[EMAIL PROTECTED]>:
> 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?christopher,this is indeed a *great* exercise... i've been using it since i can'tremember when... i had it as an exercise in BASIC and Pascal when i
was in high school, then turned it into an exercise for my C andPython courses.  naturally it's also in Core Python.others have said it and given code already, but this problem breaksdown into 3 discrete steps:
1. give values to R, P or S, i.e., an int, char, or some constant2. have the user choose one via its constant3. have the computer choose another -- you will need to randomly pickone of the 3 constants
4. logic to determine who the "winner" is of if it is a drawgood luck!-- wesley- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.comwesley.j.chun :: wescpy-at-gmail.compython training and technical consultingcyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com___Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rock, Paper, Scissors

2006-08-12 Thread Robert Wierschke
sorry I answerd to

Message: 2
Date: Thu, 10 Aug 2006 16:39:41 -0700 (PDT)
From: Christopher Spears <[EMAIL PROTECTED]>
Subject: [Tutor] Rock, Paper, Scissors


and his else has a condition

>else human.choice == 'scissors' and computer.choice
>== 'rocks':
>print "Computer wins!"
>computer.points = computer.points + 1



Luke Paireepinart schrieb:
> Robert Wierschke wrote:
>> the else part can't have a condition!!!
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>   
> Are you referring to Alan's post?
> You should have included the original text in your reply so we would
> know what you're talking about.
> If  you _are_ talking about Alan's example:
> #---
>
> outcome = results[computer.choice][human.choice]
> if outcome == 0: print "Computer Wins"
> elif outcome == 1: print "Human wins"
> else: print 'Draw!'
> #---
> his else doesn't have a condition.
> it's just that he only needed to do one thing in the else clause
> and it looks neater to put it on one line.
> else: print 'Draw!'
> is equivalent to
> else:
> print 'Draw!'
>
>

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rock, Paper, Scissors

2006-08-11 Thread Luke Paireepinart

> He wasn't. From the original post:
>
>   elif human.choice == 'rocks' and computer.choice ==
> 'scissors':
>   print "Human wins!"
>   human.points = human.points + 1
>   else human.choice == 'scissors' and computer.choice
> == 'rocks':
>   print "Computer wins!"
>   computer.points = computer.points + 1
>
>
>   
ah.
thanks for clearing this up.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rock, Paper, Scissors

2006-08-11 Thread Ismael Garrido
Luke Paireepinart escribió:
> Robert Wierschke wrote:
>   
>> the else part can't have a condition!!!
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>   
>> 
> Are you referring to Alan's post?
> You should have included the original text in your reply so we would 
> know what you're talking about.
He wasn't. From the original post:

elif human.choice == 'rocks' and computer.choice ==
'scissors':
print "Human wins!"
human.points = human.points + 1
else human.choice == 'scissors' and computer.choice
== 'rocks':
print "Computer wins!"
computer.points = computer.points + 1



Ismael
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] rock, paper, scissors

2006-08-11 Thread Christopher Spears
--- Tom Wilson <[EMAIL PROTECTED]> wrote:

> hi,
> 
> could you please explain to me how your rock paper
> scissors game script 
> works because i am a bit confused.
> 
> thanks
> tom
> 
> 

The game does not work in its current form, which may
be some cause for confusion.  :-)

In designing the program, I took the object oriented
route.  Why?  When I started to design the game, I
imagined two characters (a human and a computer)
playing against each other.  Each character has
similar attributes, i.e. points and a choice, and
behavoir.  They play.  The behavoir is implemented
differently for each character because how a computer
makes a decision is different from how a human makes a
decision.

After the characters decide which object to pick, a
function called compare_objects compares the objects
and uses logic to determine the winner.  After the
winner is picked, points are incremented and play
continues until one character reaches a set number of
points.

The two problem areas are the compare_objects function
and the while loop.  They don't work.  (At least, not
well.)  However, I have gotten several useful tips
from tutors, so I will be making a lot of progress
next week!


-Chris


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rock, Paper, Scissors

2006-08-11 Thread wesley chun
> 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?


christopher,

this is indeed a *great* exercise... i've been using it since i can't
remember when... i had it as an exercise in BASIC and Pascal when i
was in high school, then turned it into an exercise for my C and
Python courses.  naturally it's also in Core Python.

others have said it and given code already, but this problem breaks
down into 3 discrete steps:

1. give values to R, P or S, i.e., an int, char, or some constant
2. have the user choose one via its constant
3. have the computer choose another -- you will need to randomly pick
one of the 3 constants
4. logic to determine who the "winner" is of if it is a draw

good luck!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rock, Paper, Scissors

2006-08-11 Thread Luke Paireepinart
Robert Wierschke wrote:
> the else part can't have a condition!!!
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   
Are you referring to Alan's post?
You should have included the original text in your reply so we would 
know what you're talking about.
If  you _are_ talking about Alan's example:
#---

outcome = results[computer.choice][human.choice]
if outcome == 0: print "Computer Wins"
elif outcome == 1: print "Human wins"
else: print 'Draw!'
#---
his else doesn't have a condition.
it's just that he only needed to do one thing in the else clause
and it looks neater to put it on one line.
else: print 'Draw!'
is equivalent to
else:
print 'Draw!'

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rock, Paper, Scissors

2006-08-11 Thread Robert Wierschke
the else part can't have a condition!!!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rock, Paper, Scissors

2006-08-11 Thread Alan Gauld
> My main concern is the compare_objects function.  Is
> there any easier way to write it?  Actually, the
> function does not work because "else condition:"
> causes a syntax error.

I'd do that with a two dimernsional table.
The table would be indexed by the computers choice and 
the human choice and the outcome stored as a value.

Thus it becomes

outcome = results[computer.choice][human.choice]
if outcome == 0: print "Computer Wins"
elif outcome == 1: print "Human wins"
else: print 'Draw!'

Another interesting variation is to create a single 
Player class and make human and computer instances.
Pass in the function for setting choice as a 
parameter to init...  That way your code 
consists of a Player class, two selection functions
and the lookup/display code that could sit in main()
This isn't any "better", as such, just different...

HTH,

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rock, Paper, Scissors

2006-08-10 Thread John Fouhy
Hi Chris,

On 11/08/06, Christopher Spears <[EMAIL PROTECTED]> wrote:
>def plays(self):
>self.choice = raw_input("Pick (R)ock, (P)aper, or
> (S)cissors! ")
>if self.choice == 'r':
>self.choice = 'rocks'
>elif self.choice == 'p':
>self.choice = 'paper'
>elif self.choice == 's':
>self.choice = 'scissors'
>else:
>print "Invalid response"

A couple of comments here ---

Firstly, to me, 'self.choice' is the thing you want to save, which is
distinct from the user's input.  So, I would call the result of
raw_input something else.  eg:

fromUser = raw_input("Pick (R)ock, (P)aper, or
(S)cissors! ")
if fromUser == 'r':
self.choice = 'rocks'
# etc

Secondly, if the user enters 'R' (as you request), your program will
say "Invalid response", because 'R' != 'r'.  You can get around this
by saying:

if fromUser.lower() == 'r':

Thirdly, a dictionary would make this much simpler.  Something like:

translate = { 'r':'rock', 's':'scissors', 'p':'paper' }
try:
self.choice = translate[fromUser.lower()]
except KeyError:
print 'Invalid response.'
# what else are you going to do?  what does self.choice become?

> def compare_objects(human, computer):
>print "Human picked ", human.choice
>print "Computer picked", computer.choice
>if human.choice == computer.choice:
>print "Draw!"
>elif human.choice == 'rocks' and computer.choice ==
> 'paper':
>print "Computer wins!"
>computer.points = computer.points + 1
[...]

You can use a similar dictionary-based approach here.  You could use a
dictionary to make a data structure that expresses what beats what,
and then reduce your code to:

if human.choice == computer.choice:
print 'Draw!'
elif # human beats computer
print 'Human wins!'
else:
print 'Computer wins!'

HTH :-)

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rock, Paper, Scissors

2006-08-08 Thread Alan Gauld
> 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?

Assign a value to rock, paper and scissors.
then use the random module to generate one of the 3 values at random.

Alan G.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rock, Paper, Scissors

2006-08-07 Thread Jordan Greenberg
Christopher Spears 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?


Since theres no real strategy to rock paper scissors, just generate a
random number from 0-2 like this:

import random

random.seed()

choice=random.randint(0, 2)
if choice==0:
#do whatever for rock
elif choice==1:
#do whatever for paper
else:
#do whatever for scissors

That'll take care of the basics for ya. Later you could try and keep
frequency counts over a bunch of trials, see if the human has a
preference, and then weight your response appropriately to try and beat
them ;)

-Jordan Greenberg

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Rock, Paper, Scissors

2006-08-07 Thread John Fouhy
On 08/08/06, Christopher Spears <[EMAIL PROTECTED]> 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.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor