Here is my Rock,Paper,Scissors script:

#!/usr/bin/python

import random
random.seed()

class Human:
        def __init__(self):
                self.points = 0
                self.choice = " "
                
        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"
        
        

class Computer:
        def __init__(self):
                self.points = 0
                self.choice = " "
        
        def plays(self):
                comp_choice = random.randint(0,2)
                if comp_choice == 0:
                        self.choice = 'rocks' #for rocks
                elif comp_choice == 1:
                        self.choice = 'paper' #for paper
                else:
                        self.choice = 'scissors' #for scissors
                        
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
        elif human.choice == 'paper' and computer.choice ==
'rocks':
                print "Human wins!"
                human.points = human.points + 1
        elif human.choice == 'scissors' and computer.choice
== 'paper':
                print "Human wins!"
                human.points = human.points + 1
        elif human.choice == 'paper' and computer.choice ==
'scissors':
                print "Computer wins!"
                computer.points = computer.points + 1
        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
                
if __name__ == "__main__":
        print "Welcome to Rock, Paper, Scissors!"
        final_points = raw_input("Play to how many points? ")
        human = Human()
        computer = Computer()
        while (human.points <= final_points or
computer.points <= final_points):
                human.plays()
                computer.plays()
                compare_objects(human, computer)
                print "Score:\tHuman: ",human.points,"\tComputer:
",computer.points
        
        
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.

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

Reply via email to