Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #main
- def playRPS(userChoice):
- compChoice = generateCompChoice()
- determineWinner(compChoice, userChoice)
- printOutcome(userChoice, compChoice, determineWinner)
- import random
- def generateCompChoice():
- #computer generates a random number
- number = random.randint(0,2)
- if number == 0:
- return "rock"
- elif number == 1:
- return "paper"
- elif number == 2:
- return "scissors"
- # returns number as a string
- # Determine the winner
- # Input compChoice, userChoice
- def determineWinner(compChoice, userChoice):
- if ((compChoice == "rock" and userChoice == "r") or (compChoice == "paper" and userChoice == "p") or (compChoice == "scissors" and userChoice == "s")):
- return "tied"
- elif ((compChoice == "rock" and userChoice == "p") or (compChoice == "paper" and userChoice == "s") or (compChoice == "scissors" and userChoice == "r")):
- return "won"
- else:
- return "lost"
- def userInput(userChoice):
- if userChoice == "r":
- return "rock"
- elif userChoice == "p":
- return "paper"
- else:
- return "scissors"
- def printOutcome(userChoice, compChoice, determineWinner):
- print("You chose " + userInput(userChoice) + " and the computer chose " + compChoice + ", so you " + determineWinner(compChoice, userChoice) + ".")
- print("Welcome to rock, paper, scissors!")
- userChoice = input("choose your item [r/p/s]:")
- playRPS(userChoice)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement