Text based version (console only) of Rock, Paper, Scissors.

# # Text based version (console only) of Rock, Paper, Scissors # # The user plays against the computer # # # In this code, 0 represents "rock", 1 represents "paper", and 2 represents "scissors" # # rock beats scissors, scissors beat paper, paper beats rock # # In the case of a tie (same thing chosen) the round is ignored (no score change) # # NOTE: There are many different CORRECT ways to code this! # My choices don't have to be your choices and may not even be the BEST choices # This code is provided only as an EXAMPLE of what can be done # Feel free to do this your OWN way!! # Declare a list to hold all the choices choice_list = ["rock", "paper", "scissors"] # Intialize scores computer_score = 0 player_score = 0 def get_computer_choice(): # Return the computer's choice of rock, paper, scissors computer_choice = random.randint(0, 2) # The statement uses the computer_choice above as an INDEX into the list # defined above. If this makes no sense, use if/elif/else to get the text # that matches the number (0=rock, 1=paper, 2=scissors) print ("\nComputer chooses " + choice_list[computer_choice].upper()) # Return the number of the computer choice return computer_choice def get_player_choice(): # Return the user's choice of rock, paper, scissors choice = input("\nWhat is your choice? Type ROCK, PAPER, or SCISSORS") # Validate the player puts in a VALID choice while choice.lower() not in choice_list: choice = input("Oops. Try again. Type ROCK, PAPER, or SCISSORS") # Returning the index number from the list defined above to turn # the text the user entered into a number # If this doesn't make sense, use if/elif/else to find the matching number # for the text they entered (0=rock, 1=paper, 2=scissors) return choice_list.index(choice) def determine_winner(player_choice, computer_choice): # Given the player_choice and computer choice, return the winner # player_choice and user_choice both have possible values # 0 (rock), 1 (paper), 2 (scissors) # rock beats scissors, scissors beat paper, paper beats rock # Return possibilities: # 0 means tie # 1 means player wins # 2 means computer wins # -1 means something weird happened relating to bad function input if player_choice == computer_choice: #TIE - no winner winner = 0 elif player_choice == 0: # player chose rock if computer_choice == 1: # computer wins because paper beats rock winner = 2 else: # rock beats everything else so player wins winner = 1 elif player_choice == 1: # player chose paper if computer_choice == 2: # computer wins because scissors beat paper winner = 2 else: # paper beats everything else so player wins winner = 1 elif player_choice == 2: # player chose scissors if computer_choice == 0: # computer wins because rock beats scissors winner = 2 else: # scissors beats everything else so player wins winner = 1 else: # An invalid player choice was entered; shouldn't happen winner = -1 return winner # MAIN print ("Welcome to Rock, Paper, Scissors!") print("\nYou will choose Rock, Paper, or Scissors") print("\nI will choose the same") print("\nThe winner is determined as follows:") print("ROCK beats SCISSORS") print("SCISSORS beat PAPER") print("PAPER beats ROCK") play_again = True while play_again: # Get user choice player_choice = get_player_choice() # Get computer choice computer_choice = get_computer_choice() # Determine which choice wins # The return value is 0 if it's a tie, 1 if the player wins, and 2 if the computer wins winner = determine_winner (player_choice, computer_choice) # Evaluate the winner number returned to keep the score up-to-date if winner == 0: print("\nTIE!! Score doesn't change") elif winner == 1: # player won player_score += 1 print("\nPlayer wins!") elif winner == 2: # computer won computer_score += 1 print("\nComputer wins!") else: # something weird happened # should never get to this path print("Hmm, something weird happened; winner number returned was " + str(winner)) print ("\nScore is now... player:" + str(player_score) + " computer: " + str(computer_score)) # Play another round, if the player wants to answer = input ("\nPlay another round? YES or NO") # Validate their input; have them retry if invalid while answer.lower() not in ["yes", "y", "no", "n"]: answer = input ("Oops try again. Enter YES or NO") if answer.lower() in ["yes", "y"]: play_again = True else: play_again = False # end while # Outside of loop that plays repeated rounds print ("\nFinal score... player:" + str(player_score) + " computer: " + str(computer_score)) print ("Thanks for playing!!")
  • Run Code
  • Show Console
  • Codesters How To (opens in a new tab)