SHARE
TWEET

Untitled

a guest Aug 24th, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3. choices = ["rock", "paper", "scissors"]
  4.  
  5. # Player 0 is the user, player 1 is the computer. An outcome matrix is
  6. # constructed below, with -1 indicating a draw, 0 means player 0 wins,
  7. # and 1 means player 1 wins. The indicies are based on the choices list
  8. # above, with index 0 = "rock", index 1 = "paper", and index 2 = "scissors".
  9. #
  10.  
  11. # Great job using a data structure in your program. 95% of people don't think to use a data structure the first time
  12. # they write this program and that is what this assignment is all about. However, I would argue that the data structure
  13. # you picked makes your code harder to read than it has to be.
  14. # If you go with a dictionary, and use strings in it i.e. "rock", "paper", and "scissors" I personally think it makes it
  15. # easier for another programmer reading your code to understand what is happening. Overall though, excellent job!
  16. outcomes = [[-1, 0, 1], [1, -1, 0], [0, 1, -1]]
  17.  
  18. game_round = 1
  19. user_wins = 0
  20. computer_wins = 0
  21.  
  22. while game_round <= 3:
  23.     user_choice = input("\nRock, paper, or scissors? ")
  24.     user_choice = user_choice.strip().lower()
  25.     try:
  26.         user_choice_index = choices.index(user_choice)
  27.     except:
  28.         print("Invalid choice!")
  29.         continue
  30.  
  31.     # Randomly choose an option for the computer
  32.     computer_choice = random.choice(choices)
  33.     print("Computer's choice: " + computer_choice)
  34.     computer_choice_index = choices.index(computer_choice) # combine line 34 and 35 into one line of code
  35.     outcome = outcomes[computer_choice_index][user_choice_index]
  36.  
  37.     # Determine the outcome for the round. Again, -1 is a draw, 0 is
  38.     # a win for the user, and 1 is a win for the computer    
  39.     if outcome == -1:
  40.         print("It's a draw!")
  41.     elif outcome == 0:
  42.         print("You win round " + str(game_round) + "!")
  43.         user_wins += 1
  44.     else:
  45.         print("The computer wins round " + str(game_round) + "!")
  46.         computer_wins += 1
  47.    
  48.     game_round += 1
  49.  
  50. # Game is over, determine the winner.
  51. print('\n')
  52. if user_wins > 1:
  53.     print("You win the game!")
  54. elif computer_wins > 1:
  55.     print("The computer wins the game!")
  56. else:
  57.     print("No winner, the game is a draw!")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top