Can someone help me with CodeHS assignment 4.7.11 Rock, Paper, Scissors

Can someone help me with CodeHS assignment 4.7.11 Rock, Paper, Scissors?

The Correct Answer and Explanation is:


Game plan (outline + hints)

  1. Import random import random
  2. Store the three moves – keep them in a list so you can pick a random one for the computer. moves = ["rock", "paper", "scissors"]
  3. Get the user’s move (use input() and immediately convert to lowercase / strip spaces).
  4. Validate the user input:
    If it isn’t “rock”, “paper”, or “scissors”, print an error and return or exit.
  5. Computer move: comp = random.choice(moves)
  6. Decide the winner
    A clean way is a dictionary that maps each move to what it beats. beats = {"rock": "scissors", "scissors": "paper", "paper": "rock"} if user == comp: outcome = "tie" elif beats[user] == comp: outcome = "user" else: outcome = "computer"
  7. Print both moves and the result in CodeHS-style wording.

Explanation

A Rock-Paper-Scissors program tests three core skills you’ve met so far in CodeHS: user input, randomness, and conditional logic. You first import Python’s random module, which supplies choice, a function that picks one element from a sequence with equal likelihood—perfect for simulating the computer’s move. Next, you create a list called moves to hold the three legal strings "rock", "paper", and "scissors". Putting them in a list brings two advantages: you can validate user entries by checking membership, and you can feed the entire list to random.choice without writing three separate if statements.

Input handling is deceptively important. Because people accidentally capitalize or add spaces, immediately converting input() to lowercase and stripping whitespace prevents subtle bugs and keeps grading scripts happy. Validating early—exiting if the user types an invalid word—simplifies the rest of the logic because you can assume the variable user is always one of the three allowed moves.

The decisive piece is determining the winner. A brute-force triple-nested if chain works, but a small dictionary is clearer and scales if you ever expand the game (e.g., Rock-Paper-Scissors-Lizard-Spock). In the dictionary beats, each key’s value is what that key defeats, so one concise comparison decides whether the user wins. Checking for a tie first is critical; otherwise both players could be reported as winners. Finally, format your printouts exactly as CodeHS expects—usually one line announcing each move and another announcing the winner—because autograders compare output character-by-character.

By following this outline and reasoning through each step, you’ll end up with clean, readable code that passes the assignment while solidifying your grasp of lists, dictionaries, randomness, and conditionals—all foundational tools for future CodeHS modules.

Scroll to Top