[Tutor] Rock, Paper, Scissors game

Alan Gauld alan.gauld at btinternet.com
Fri Jul 26 20:58:43 CEST 2013


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/



More information about the Tutor mailing list