Rock Paper Scissors

Rock Paper Scissors

Write a function that decides the winner of a round of rock-paper-scissors.

rps_winner(player1, player2)

Each argument is one of "rock", "paper", or "scissors". Rock beats scissors, scissors beats paper, and paper beats rock. Return "player1", "player2", or "tie".

Examples

rps_winner("rock", "scissors")     // → "player1"
rps_winner("paper", "rock")        // → "player1"
rps_winner("scissors", "rock")     // → "player2"
rps_winner("rock", "rock")         // → "tie"

Walkthrough

Thinking it through

The tempting first approach is a long chain of if/elif covering every combination — rock vs scissors, rock vs paper, paper vs rock, and so on. That's 9 combinations total (3 choices × 3 choices), which gets unwieldy fast and is easy to get wrong.

A cleaner approach: notice that the rule itself ("rock beats scissors, scissors beats paper, paper beats rock") is really just data — a lookup from each move to the move it beats:

def rps_winner(player1, player2):
    if player1 == player2:
        return "tie"
    beats = {"rock": "scissors", "scissors": "paper", "paper": "rock"}
    if beats[player1] == player2:
        return "player1"
    return "player2"

First, handle the tie case directly — if both players picked the same move, there's no winner, full stop.

Otherwise, look up what player1's move beats. If that happens to equal player2's move, player1 wins. If it doesn't, there's only one possibility left: since the moves aren't equal (already ruled out) and player1 doesn't beat player2, player2 must beat player1 — no further lookup needed, just return "player2".

This is a preview of an idea you'll use a lot going forward: when a rule is really a fixed set of facts ("X beats Y"), storing it as data in a dict is often clearer — and easier to extend later — than hardcoding it as a long chain of conditionals.

main.py
Console
Press Run to execute your code, or Submit to test and complete this problem.