Advertisement
afeyajahin

RPS problem

Sep 15th, 2020
1,301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. #main
  2. def playRPS(userChoice):
  3.     compChoice = generateCompChoice()
  4.     determineWinner(compChoice, userChoice)
  5.     printOutcome(userChoice, compChoice, determineWinner)
  6.  
  7. import random
  8. def generateCompChoice():
  9. #computer generates a random number
  10.     number = random.randint(0,2)
  11.     if number == 0:
  12.         return "rock"
  13.     elif number == 1:
  14.         return "paper"
  15.     elif number == 2:
  16.         return "scissors"
  17. # returns number as a string
  18.  
  19. # Determine the winner
  20. # Input compChoice, userChoice
  21. def determineWinner(compChoice, userChoice):
  22.     if ((compChoice == "rock" and userChoice == "r") or (compChoice == "paper" and userChoice == "p") or (compChoice == "scissors" and userChoice == "s")):
  23.         return "tied"
  24.     elif ((compChoice == "rock" and userChoice == "p") or (compChoice == "paper" and userChoice == "s") or (compChoice == "scissors" and userChoice == "r")):
  25.         return "won"
  26.     else:
  27.         return "lost"
  28.  
  29. def userInput(userChoice):
  30.     if userChoice == "r":
  31.         return "rock"
  32.     elif userChoice == "p":
  33.         return "paper"
  34.     else:
  35.         return "scissors"
  36. def printOutcome(userChoice, compChoice, determineWinner):
  37.     print("You chose " + userInput(userChoice) + " and the computer chose " + compChoice + ", so you " + determineWinner(compChoice, userChoice) + ".")
  38.  
  39. print("Welcome to rock, paper, scissors!")
  40. userChoice = input("choose your item [r/p/s]:")
  41. playRPS(userChoice)
  42.  
  43.  
  44.                  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement