Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # main
- import random
- def playRPSRound(userChoice):
- """
- Given a string with the user's choice
- ("r" for rock, "p" for paper, "s" for scissors),
- randomly choose an item for the computer and resolve the round.
- Return "won" if the user won, "tied" if it is a tie and "lose" if the user lost."""
- #Have computer make a choice
- compChoice = generateCompChoice()
- #invoke determineWinner for outcome
- outcome = determineWinner(compChoice, userChoice)
- #invoke printOutcome to print the round result)
- printOutcome(userChoice, compChoice, outcome)
- generateScores(outcome)
- printScores()
- #return the outcome
- return outcome
- def generateCompChoice():
- """
- Have the computer randomly generate a number and return
- "rock" for 0, "paper" for 1, or "scissors" for 2
- """
- #choose a random number [0,1,2]
- number = random.randint(0, 2)
- # if it's a 0
- if number == 0:
- return "rock"
- # if it's a 1
- elif number == 1:
- return "paper"
- # if it's a 2
- else:
- return "scissors"
- # Determine the winner
- # Input compChoice, userChoice as parameters
- def determineWinner(compChoice, userChoice):
- """Compare user's Input with computer's choice to determine if user 'lost', 'won', or 'tied' """
- # if they choose the same item, user 'tied'
- if ((compChoice == "rock" and userChoice == "r") or (compChoice == "paper" and userChoice == "p") or (compChoice == "scissors" and userChoice == "s")):
- return "tied"
- # if computer-user is r-p , p-s, or s-r, user 'won'
- elif ((compChoice == "rock" and userChoice == "p") or (compChoice == "paper" and userChoice == "s") or (compChoice == "scissors" and userChoice == "r")):
- return "won"
- #otherwise, user 'lost'
- else:
- return "lost"
- def userInput(userChoice):
- """Given "r", return "rock", "p" --> "paper", "s" --> "scissors"""
- # expand 'r' --> 'rock'
- if userChoice == "r":
- return "rock"
- # expand 'p'--> 'paper'
- elif userChoice == "p":
- return "paper"
- # expand 's' --> "scissors"
- else:
- return "scissors"
- def printOutcome(userChoice, compChoice, outcome):
- """print scores"""
- print("You chose " + userInput(userChoice) + " and the computer chose " + compChoice + ", so you " +outcome +"!")
- def generateScores(outcome):
- """takes in outcome ('won', 'lose' or 'tie' and calculates userScore
- "won" -> 10 if won last round, 15 if last 2 rounds, 20 if last 3 rounds...so on
- "lose", "tie" -> 0 """
- # access global variables
- global userScore
- global compScore
- global userWinsInARow
- global compWinsInARow
- # if user wins
- if outcome == "won":
- # update score count
- userWinsInARow = userWinsInARow + 1
- # computer lost so make userScore 0
- compWinsInARow = 0
- # compute new score
- userScore += 5 + 5*userWinsInARow
- # if user lost
- elif outcome == "lost":
- # update score count
- compWinsInARow +=1
- # user lost so make score 0
- userWinsInARow = 0
- # compute new compScore
- compScore += 5 + 5*compWinsInARow
- else:
- # no one won, so clear wins
- userWinsInARow = 0
- compWinsInARow = 0
- def printScores():
- " " " Print the scores " " "
- print("Your Score: " + str(userScore) + ", Computer's Score: " + str(compScore))
- # use these global variables to track the scores
- userScore = 0 # user's score starts at 0
- compScore = 0 # computer's score starts at 0
- userWinsInARow = 0 # keep track of number of user wins in a row
- compWinsInARow = 0 # keep track of number of comp wins in a row
- #print statement to welcome users
- print("Welcome to rock, paper, scissors!")
- # take input from user and then play the round ( 5 times)
- userChoice = input("Round 1-choose your item [r/p/s] : ")
- playRPSRound(userChoice)
- userChoice = input("Round 2-choose your item [r/p/s] : ")
- playRPSRound(userChoice)
- userChoice = input("Round 3-choose your item [r/p/s] : ")
- playRPSRound(userChoice)
- userChoice = input("Round 4-choose your item [r/p/s] : ")
- playRPSRound(userChoice)
- userChoice = input("Round 5-choose your item [r/p/s] : ")
- playRPSRound(userChoice)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement