Codehs 4.7 11 Rock Paper Scissors
madrid
Mar 18, 2026 · 4 min read
Table of Contents
CodeHS 4.7 11 Rock Paper Scissors: A Complete Guide for Beginners
CodeHS 4.7 11 Rock Paper Scissors is one of the most widely used introductory programming assignments in the CodeHS curriculum. It helps students practice fundamental concepts such as user input, random number generation, conditional statements, and loops while building a fun, interactive game. This article walks through the entire exercise, explains the underlying logic, offers step‑by‑step implementation tips, and suggests ways to extend the project for deeper learning.
Understanding the Assignment
In CodeHS unit 4.7, lesson 11, learners are asked to create a text‑based version of the classic Rock‑Paper‑Scissors game where the computer makes a random choice and the user enters their move. The program must:
- Prompt the user to type rock, paper, or scissors.
- Generate a random choice for the computer.
- Compare the two selections and announce the winner (or a tie).
- Optionally allow the user to play again until they decide to quit.
The exercise reinforces several core programming ideas:
- Input handling – reading a string from the console.
- Randomization – using a random number generator to simulate the computer’s move. - Conditionals – evaluating win/lose/tie scenarios.
- Loops – repeating the game based on user preference. - Functions – encapsulating logic for readability and reuse.
Setting Up the Environment
Before writing code, make sure you have the CodeHS editor open for the JavaScript (or Python) track, depending on the language your class uses. The starter file typically contains a main() function and a comment indicating where to place your solution.
If you are working locally, you can use any IDE or even a simple text editor with a terminal. For JavaScript, Node.js will run the script; for Python, the standard interpreter suffices.
Step‑by‑Step Implementation
Below is a language‑agnostic outline that you can translate into either JavaScript or Python. Each step includes the key constructs you’ll need.
1. Pseudocode ```
START DISPLAY welcome message REPEAT GET userChoice from input (lowercase) IF userChoice not in ["rock","paper","scissors"] DISPLAY error message and ask again END IF UNTIL userChoice is valid
SET computerChoice to random element from ["rock","paper","scissors"]
DISPLAY both choices
IF userChoice == computerChoice
DISPLAY "It's a tie!"
ELSE IF (userChoice == "rock" AND computerChoice == "scissors") OR
(userChoice == "paper" AND computerChoice == "rock") OR
(userChoice == "scissors" AND computerChoice == "paper")
DISPLAY "You win!"
ELSE
DISPLAY "Computer wins!"
END IF
ASK user if they want to play again
IF answer is "yes" or "y"
LOOP back to start
ELSE
DISPLAY farewell message
END IF
END
#### 2. Translating to JavaScript
```javascript
function start() {
println("Welcome to Rock Paper Scissors!");
let playAgain = true;
while (playAgain) {
// ----- Get valid user input -----
let userChoice;
do {
userChoice = readLine("Enter rock, paper, or scissors: ").toLowerCase();
if (!["rock","paper","scissors"].includes(userChoice)) {
println("Invalid choice. Please try again.");
}
} while (!["rock","paper","scissors"].includes(userChoice));
// ----- Generate computer choice -----
const choices = ["rock","paper","scissors"];
const randomIndex = Randomizer.nextInt(0, choices.length); // 0,1,2
const computerChoice = choices[randomIndex];
println(`You chose: ${userChoice}`);
println(`Computer chose: ${computerChoice}`);
// ----- Determine the outcome -----
if (userChoice === computerChoice) {
println("It's a tie!");
} else if (
(userChoice === "rock" && computerChoice === "scissors") ||
(userChoice === "paper" && computerChoice === "rock") ||
(userChoice === "scissors" && computerChoice === "paper")
) {
println("You win!");
} else {
println("Computer wins!");
}
// ----- Ask to play again -----
const answer = readLine("Play again? (yes/no): ").toLowerCase();
playAgain = (answer === "yes" || answer === "y");
}
println("Thanks for playing! Goodbye.");
}
3. Translating to Python
import randomdef main():
print("Welcome to Rock Paper Scissors!")
play_again = True while play_again:
# ----- Get valid user input -----
user_choice = ""
while user_choice not in ["rock", "paper", "scissors"]:
user_choice = input("Enter rock, paper, or scissors: ").strip().lower()
if user_choice not in ["rock", "paper", "scissors"]:
print("Invalid choice. Please try again.")
# ----- Generate computer choice ----- choices = ["rock", "paper", "scissors"]
computer_choice = random.choice(choices)
print(f"You chose: {user_choice}")
print(f"Computer chose: {computer_choice}")
# ----- Determine the outcome ----- 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!")
# ----- Ask to play again -----
again = input("Play again? (yes/no): ").strip().lower()
play_again = again in ["yes", "y"]
print("Thanks for playing! Goodbye.")
if __name__ == "__main__":
main()
Detailed Code Explanation
| Section | Purpose | Key Concepts |
|---|---|---|
| Welcome message | Sets context for the user. | println / print |
| Input validation loop | Guarantees the user types a valid move |
Latest Posts
Latest Posts
-
In A Civil Case For Conversion You Can Request
Mar 18, 2026
-
Sugarcane Is Vulnerable To The Cane Beetle
Mar 18, 2026
-
Draw The Structure For Compound A
Mar 18, 2026
-
What Action Does The Highlighted Muscle Have At The Wrist
Mar 18, 2026
-
Select The Appropriate Term For Its Respective Definition
Mar 18, 2026
Related Post
Thank you for visiting our website which covers about Codehs 4.7 11 Rock Paper Scissors . We hope the information provided has been useful to you. Feel free to contact us if you have any questions or need further assistance. See you next time and don't miss to bookmark.