# Name: Example Solutions # Homework 1, Exercise 2.2: rps.py # Truth Table # Player 1 Player 2 Result # ------------------------------- # Rock Rock Tie # Rock Scissors Player 1 # Rock Paper Player 2 # Scissors Rock Player 2 # Scissors Scissors Tie # Scissors Paper Player 1 # Paper Rock Player 1 # Paper Scissors Player 2 # Paper Paper Tie # Note: You could just have 10 statements in an if/elif/else structure, but # if you look at the cases, you can simplify it down, which is what I've # done below. # Get the user's input print "Please enter the players' choices (choose from rock, paper, and scissors):" player1 = raw_input("Player 1? ") player2 = raw_input("Player 2? ") # Validate the input (must be one of "rock", "paper", or "scissors") # (This could also be after all other checks, but this way you know that # if the game is played, both inputs are valid.) if (player1 != "rock" and player1 != "paper" and player1 != "scissors") or \ (player2 != "rock" and player2 != "paper" and player2 != "scissors"): print "Invalid input. The players must choose from 'rock', 'paper', and 'scissors'." # If the input was valid, go ahead and continue else: # It's a tie if the players picked the same value if player1 == player2: print "Tie!" # Situations where player 1 wins elif player1 == "rock" and player2 == "scissors" or \ player1 == "scissors" and player2 == "paper" or \ player1 == "paper" and player2 == "rock": print "Player 1 wins." # If it's not a tie and player 1 didn't win, then player 2 must win else: print "Player 2 wins."