[Tutor] rockpaperscissors

ahimsa ahimsa@onetel.net.uk
Mon Jan 27 14:15:04 2003


Hello Magnus

On Sun, 2003-01-26 at 23:07, Magnus Lycka wrote:


> For the algorithm, this little session might give some ideas:
> 
>  >>> winners = {'scissors': 'rock', 'paper': 'scissors', 'rock': 'paper'}
>  >>> def whoWon(a, b):
> ...     if winners[a] == b:
> ...             print b, "wins over", a
> ...     elif winners[b] == a:
> ...             print a, "wins over", b
> ...     else:
> ...             print "A draw, try again"
> ...
>  >>> whoWon('rock', 'scissors')
> rock wins over scissors
>  >>> whoWon('rock', 'paper')
> paper wins over rock
>  >>> whoWon('rock', 'rock')
> A draw, try again
> 
> Maybe whoWon should instead take a tuple of (name, choice)?

So you are suggesting a dictionary where the key pairs are the win-lose
pairs (i.e. 'scissors' broken by 'rock', etc). Sean also suggested a
dictionary approach, and again I must say that it does give a really
nice neat look to the code, and is easy to read too. Thank you for your
suggestion; I am certainly more inclined to try to use that approach.
All the best

Andrew

-- 
ahimsa <ahimsa@onetel.net.uk>