[Tutor] Rock, Paper, Scissors game

Saad Javed sbjaved at gmail.com
Fri Jul 26 13:22:50 CEST 2013


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?
    print "Player wins"
  else:
    print "CPU wins"
else:
  print "tie!"

Thanks.


More information about the Tutor mailing list