Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- choices = ["rock", "paper", "scissors"]
- # Player 0 is the user, player 1 is the computer. An outcome matrix is
- # constructed below, with -1 indicating a draw, 0 means player 0 wins,
- # and 1 means player 1 wins. The indicies are based on the choices list
- # above, with index 0 = "rock", index 1 = "paper", and index 2 = "scissors".
- #
- # Great job using a data structure in your program. 95% of people don't think to use a data structure the first time
- # they write this program and that is what this assignment is all about. However, I would argue that the data structure
- # you picked makes your code harder to read than it has to be.
- # If you go with a dictionary, and use strings in it i.e. "rock", "paper", and "scissors" I personally think it makes it
- # easier for another programmer reading your code to understand what is happening. Overall though, excellent job!
- outcomes = [[-1, 0, 1], [1, -1, 0], [0, 1, -1]]
- game_round = 1
- user_wins = 0
- computer_wins = 0
- while game_round <= 3:
- user_choice = input("\nRock, paper, or scissors? ")
- user_choice = user_choice.strip().lower()
- try:
- user_choice_index = choices.index(user_choice)
- except:
- print("Invalid choice!")
- continue
- # Randomly choose an option for the computer
- computer_choice = random.choice(choices)
- print("Computer's choice: " + computer_choice)
- computer_choice_index = choices.index(computer_choice) # combine line 34 and 35 into one line of code
- outcome = outcomes[computer_choice_index][user_choice_index]
- # Determine the outcome for the round. Again, -1 is a draw, 0 is
- # a win for the user, and 1 is a win for the computer
- if outcome == -1:
- print("It's a draw!")
- elif outcome == 0:
- print("You win round " + str(game_round) + "!")
- user_wins += 1
- else:
- print("The computer wins round " + str(game_round) + "!")
- computer_wins += 1
- game_round += 1
- # Game is over, determine the winner.
- print('\n')
- if user_wins > 1:
- print("You win the game!")
- elif computer_wins > 1:
- print("The computer wins the game!")
- else:
- print("No winner, the game is a draw!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement