[Tutor] Rock, Paper, Scissors

Alan Gauld alan.gauld at freenet.co.uk
Fri Aug 11 12:13:26 CEST 2006

> 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...


Alan G.

More information about the Tutor mailing list