Writing a Python program for the classic hand game is a common milestone in introductory programming courses. If you're stuck on CodeHS 4.7.11 Rock Paper Scissors, you likely need a clear breakdown of user input handling, random computer choices, and conditional statements to determine the winner.
This exercise tests your ability to map user inputs (like "rock", "paper", or "scissors") against randomized computer choices while handling ties and rule validation correctly. Let's walk through how to build a clean, working solution from scratch and avoid the common logic traps that break this specific coding exercise.
Understanding the Exercise Requirements
Before writing any code, it's vital to know what the CodeHS automated grader expects. Exercise 4.7.11 typically requires a program that prompts the user for their choice, generates a random choice for the computer, compares the two values, and prints the winner.
The core rules of the game are straightforward:
- Rock beats scissors.
- Scissors beat paper.
- Paper beats rock.
- Identical choices result in a tie.
CodeHS exercises often rely on specific function names, prompt strings, or imported libraries. For Python units covering conditionals and functions (such as Unit 4), you'll need to import the built-in random module to handle the computer's turn.
Setting Up User Input and Randomization
The first step in your script is capturing what the user wants to play and generating the computer's move. You'll want to prompt the user using input() and ensure your code accounts for lowercase or uppercase variations to prevent input errors.
import random
def get_computer_choice():
# Generate a random integer or pick from a list
choices = ["rock", "paper", "scissors"]
return random.choice(choices)
When prompting the user, keep the text clean. If the automated test cases expect a specific prompt like "Enter your choice: ", make sure you match it exactly, as strict string matching causes many students to fail tests even when their logic is correct.
Writing the Winning Logic
Once you have both choices, you need a series of conditional statements (if, elif, and else) to evaluate who won.
def play_rock_paper_scissors():
user_choice = input("Enter rock, paper, or scissors: ").lower()
computer_choice = get_computer_choice()
print(f"Computer chose: {computer_choice}")
if user_choice == computer_choice:
print("It's a tie!")
elif (user_choice == "rock" and computer_choice == "scissors") or \
(user_choice == "paper" and computer_choice == "rock") or \
(user_choice == "scissors" and computer_choice == "paper"):
print("You win!")
else:
print("Computer wins!")
Grouping your winning conditions with logical operators (and/or) keeps your function compact. Alternatively, you can use nested if statements if your instructor prefers that structure for readability. For more details on Python's control flow statements, check out the official Python Documentation.
Common Mistakes to Avoid
Students often encounter specific roadblocks while working through this CodeHS module. Keep these pitfalls in mind to save debugging time:
- Case Sensitivity: If a user types "Rock" with a capital R, a direct comparison like
user_choice == "rock"will evaluate to false. Always chain.lower()to your input function. - Typo in Output Strings: CodeHS automated tests frequently check for exact output strings like
"You win!"or"It's a tie!". Extra spaces, missing punctuation, or different phrasing will cause test failures. - Forgetting the Import: If you forget to write
import randomat the top of your file, Python will throw aNameErrorthe moment the script tries to pick a move for the computer.
To test your code locally before submitting it to the platform, you can run your script inside a terminal or check out beginner tutorials on Real Python for syntax reminders.
Frequently Asked Questions
Why does my code fail CodeHS tests even when it works?
Automated graders look for exact matches in input prompts and output strings. Check your spelling, capitalization, and punctuation against the instructions provided in the exercise prompt.
How do I handle invalid user input?
You can wrap your input logic in a while loop that continues to prompt the user until they type either "rock", "paper", or "scissors", rejecting any unexpected strings.
Can I use numbers instead of strings for choices?
Some versions of this exercise map rock, paper, and scissors to numbers (e.g., 0 for rock, 1 for paper, 2 for scissors). Check your specific lesson instructions to see if string inputs or integer inputs are required.