petew71

rps.py

Jul 29th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. import random
  2. def userChoice():
  3.     """ Asks user for choice of input. If input is correct, Returns choice ."""
  4.     userChoice = raw_input("Do you choose rock, paper or scissors?").lower()
  5.  
  6.     while userChoice != "rock"\
  7.         and userChoice != "paper"\
  8.         and userChoice != "scissors":
  9.         userChoice = raw_input("Do you choose rock, paper or scissors?").lower()
  10.        
  11.     return userChoice;
  12.  
  13.  
  14. def computerChoice():
  15.     """ Randomly generates the computers choice."""
  16.     computerChoice = random.randrange(0, 3)
  17.     if computerChoice == 0:
  18.         computerChoice = "rock"
  19.     elif computerChoice == 1:
  20.         computerChoice = "paper"
  21.     else:
  22.         computerChoice = "scissors"
  23.     print "computers choice =",computerChoice
  24.     return computerChoice
  25.  
  26.  
  27. def compare(choice1, choice2):
  28.     """ Takes two choices as parameters and compares them.
  29.        A result is returned in the form of "a tie!" or the winning choice."""
  30.            
  31.     if choice1 == choice2:
  32.        
  33.         return "The result is a tie!"
  34.    
  35.     if choice1 == "rock":
  36.         if choice2 == "scissors":
  37.             return "rock wins";
  38.         else:
  39.             return "paper wins";
  40.        
  41.    
  42.     if choice1 == "paper":
  43.         if choice2 == "rock":
  44.             return "paper wins"
  45.         else:
  46.             return "scissors wins"
  47.            
  48.        
  49.    
  50.     if choice1 == "scissors":
  51.         if choice2 == "rock":
  52.             return "rock wins ";
  53.         else:
  54.             return "scissors wins";
  55.      
  56. # start the program by calling compare, passing userChoice and computerChoice functions as parameters.
  57. print compare(userChoice(), computerChoice())
Advertisement
Add Comment
Please, Sign In to add comment