Advertisement
afeyajahin

RPSproblem.py

Sep 21st, 2020 (edited)
969
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.33 KB | None | 0 0
  1. # main
  2. import random
  3.  
  4. def playRPSRound(userChoice):
  5.     """
  6.    Given a string with the user's choice
  7.    ("r" for rock, "p" for paper, "s" for scissors),
  8.    randomly choose an item for the computer and resolve the round.
  9.    Return "won" if the user won, "tied" if it is a tie and "lose" if the user lost."""
  10.  
  11.     #Have computer make a choice
  12.     compChoice = generateCompChoice()
  13.  
  14.     #invoke determineWinner for outcome
  15.     outcome = determineWinner(compChoice, userChoice)
  16.    
  17.     #invoke printOutcome to print the round result)
  18.     printOutcome(userChoice, compChoice, outcome)
  19.    
  20.     generateScores(outcome)
  21.     printScores()
  22.    
  23.     #return the outcome
  24.     return outcome
  25.    
  26.    
  27. def generateCompChoice():
  28.     """
  29.    Have the computer randomly generate a number and return
  30.    "rock" for 0, "paper" for 1, or "scissors" for 2
  31.    """
  32.     #choose a random number [0,1,2]
  33.     number = random.randint(0, 2)
  34.     # if it's a 0
  35.     if number == 0:
  36.         return "rock"
  37.     # if it's a 1
  38.     elif number == 1:
  39.         return "paper"
  40.     # if it's a 2
  41.     else:
  42.         return "scissors"
  43.  
  44.  
  45.  
  46. # Determine the winner
  47. # Input compChoice, userChoice as parameters
  48. def determineWinner(compChoice, userChoice):
  49.     """Compare user's Input with computer's choice to determine if user 'lost', 'won', or 'tied' """
  50.     # if they choose the same item, user 'tied'
  51.     if ((compChoice == "rock" and userChoice == "r") or (compChoice == "paper" and userChoice == "p") or (compChoice == "scissors" and userChoice == "s")):
  52.         return "tied"
  53.     # if computer-user is  r-p , p-s, or s-r, user 'won'
  54.     elif ((compChoice == "rock" and userChoice == "p") or (compChoice == "paper" and userChoice == "s") or (compChoice == "scissors" and userChoice == "r")):
  55.         return "won"
  56.     #otherwise, user 'lost'
  57.     else:
  58.         return "lost"
  59.  
  60.  
  61. def userInput(userChoice):
  62.     """Given "r", return "rock", "p" --> "paper", "s" --> "scissors"""
  63.     # expand 'r' --> 'rock'
  64.     if userChoice == "r":
  65.         return "rock"
  66.     # expand 'p'--> 'paper'
  67.     elif userChoice == "p":
  68.         return "paper"
  69.     # expand 's' --> "scissors"
  70.     else:
  71.         return "scissors"
  72.  
  73.  
  74. def printOutcome(userChoice, compChoice, outcome):
  75.     """print scores"""
  76.     print("You chose " + userInput(userChoice) + " and the computer chose " + compChoice + ", so you " +outcome +"!")
  77.    
  78.  
  79.  
  80. def generateScores(outcome):
  81.     """takes in outcome ('won', 'lose' or 'tie'  and calculates userScore
  82.      "won" -> 10 if won last round, 15 if last 2 rounds, 20 if last 3 rounds...so on
  83.      "lose", "tie" -> 0 """
  84.     # access global variables
  85.     global userScore
  86.     global compScore
  87.     global userWinsInARow
  88.     global compWinsInARow
  89.     # if user wins
  90.     if outcome == "won":
  91.         # update score count
  92.         userWinsInARow = userWinsInARow + 1
  93.         # computer lost so make userScore 0
  94.         compWinsInARow = 0
  95.         # compute new score
  96.         userScore += 5 + 5*userWinsInARow
  97.     # if user lost
  98.     elif outcome == "lost":
  99.         # update score count
  100.         compWinsInARow +=1
  101.         # user lost so make score 0
  102.         userWinsInARow = 0
  103.         # compute new compScore
  104.         compScore += 5 + 5*compWinsInARow
  105.        
  106.     else:
  107.         # no one won, so clear wins
  108.          userWinsInARow = 0
  109.          compWinsInARow = 0
  110.      
  111.  
  112. def printScores():
  113.     " " " Print the scores " " "
  114.     print("Your Score: " + str(userScore) + ", Computer's Score: " + str(compScore))
  115.    
  116.      
  117. # use these global variables to track the scores
  118. userScore = 0 # user's score starts at 0
  119. compScore = 0 # computer's score starts at 0
  120. userWinsInARow = 0 # keep track of number of user wins in a row
  121. compWinsInARow = 0 # keep track of number of comp wins in a row
  122.  
  123.      
  124.  
  125. #print statement to welcome users
  126. print("Welcome to rock, paper, scissors!")
  127. # take input from user and then play the round ( 5 times)
  128. userChoice = input("Round 1-choose your item [r/p/s] : ")
  129. playRPSRound(userChoice)
  130. userChoice = input("Round 2-choose your item [r/p/s] : ")
  131. playRPSRound(userChoice)
  132. userChoice = input("Round 3-choose your item [r/p/s] : ")
  133. playRPSRound(userChoice)
  134. userChoice = input("Round 4-choose your item [r/p/s] : ")
  135. playRPSRound(userChoice)
  136. userChoice = input("Round 5-choose your item [r/p/s] : ")
  137. playRPSRound(userChoice)
  138.  
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement