Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- def userChoice():
- """ Asks user for choice of input. If input is correct, Returns choice ."""
- userChoice = raw_input("Do you choose rock, paper or scissors?").lower()
- while userChoice != "rock"\
- and userChoice != "paper"\
- and userChoice != "scissors":
- userChoice = raw_input("Do you choose rock, paper or scissors?").lower()
- return userChoice;
- def computerChoice():
- """ Randomly generates the computers choice."""
- computerChoice = random.randrange(0, 3)
- if computerChoice == 0:
- computerChoice = "rock"
- elif computerChoice == 1:
- computerChoice = "paper"
- else:
- computerChoice = "scissors"
- print "computers choice =",computerChoice
- return computerChoice
- def compare(choice1, choice2):
- """ Takes two choices as parameters and compares them.
- A result is returned in the form of "a tie!" or the winning choice."""
- if choice1 == choice2:
- return "The result is a tie!"
- if choice1 == "rock":
- if choice2 == "scissors":
- return "rock wins";
- else:
- return "paper wins";
- if choice1 == "paper":
- if choice2 == "rock":
- return "paper wins"
- else:
- return "scissors wins"
- if choice1 == "scissors":
- if choice2 == "rock":
- return "rock wins ";
- else:
- return "scissors wins";
- # start the program by calling compare, passing userChoice and computerChoice functions as parameters.
- print compare(userChoice(), computerChoice())
Advertisement
Add Comment
Please, Sign In to add comment