CodeHS Apples Oranges Answers

CodeHS Apples Oranges Answers

The CodeHS “Apples and Oranges” exercise (often categorized as 3.4.4 in JavaScript or 3.4.6 in Python) is designed to teach the fundamentals of declaring variables, updating variable values, and printing output by managing the count of fruit.

Category:

/ 7
CodeHS — Unit 3 Answer Key
Apples and Oranges (3.4.6) + All Unit 3 Exercises | Python & JavaScript | 2024/2025
Complete verified answer key for CodeHS Unit 3 — Programming with Python and JavaScript. Covers all
exercises including the featured Apples and Oranges (3.4.6), Your Name and Hobby, Obi-Wan Says, Sporting
Goods Shop, Running Speed, Graphics exercises (French Flag, Snowman), Mouse Events, and more. Both
Python and JavaScript solutions provided.
Exercise # Title Language
3.3.4 Your Name and Hobby Python & JavaScript
3.4.6 Apples and Oranges (MAIN FOCUS) Python & JavaScript
3.5.4 Obi-Wan Says Python & JavaScript
3.6.7 Sporting Goods Shop Python & JavaScript
3.6.8 Running Speed Python
3.7.4 Blue Circle (Graphics) Python
3.7.5 Red Rectangle (Graphics) Python
3.7.8 French Flag (Graphics) Python
3.7.9 Snowman (Graphics) Python
3.8.5 Click for Rectangles (Mouse Events) Python
Key Concepts: Variables & str() Reference
Common Errors & How to Fix Them Reference
Exercise 3.3.4: Your Name and Hobby
Task: Print your name and a hobby you have. Use print() for each line.
Python Solution:
# This program prints your name and a hobby
print("My name is Alex")
print("I like to play basketball")
Expected Output:
My name is Alex
I like to play basketball
JavaScript Solution:
function start() {
println("My name is Alex.");
println("I like to play basketball.");
}
Tip: Use two separate print() calls — one for your name, one for your hobby. You can replace the example name and
hobby with your own.
Exercise 3.4.6: Apples and Oranges — MAIN EXERCISE
Task: Initialize two variables to store the number of apples (20) and oranges (15) in your store. Print how many
you have of each. Then set the number of oranges to 0 and print the values again. Required output format: Number
of apples: [value] Number of oranges: [value] Number of apples: [value] Number of oranges: [value]
Python Solution (Clean & Correct):
# Enter your code here
num_apples = 20
print("Number of apples: " + str(num_apples))
num_oranges = 15
print("Number of oranges: " + str(num_oranges))
# Reprint apples (unchanged)
print("Number of apples: " + str(num_apples))
# Set oranges to 0 and reprint
num_oranges = 0
print("Number of oranges: " + str(num_oranges))
Expected Output:
Number of apples: 20
Number of oranges: 15
Number of apples: 20
Number of oranges: 0
Alternative Python Solution (also accepted):
# Alternative — using comma in print instead of string concatenation
num_apples = 20
num_oranges = 15
print("Number of apples:", num_apples)
print("Number of oranges:", num_oranges)
print("Number of apples:", num_apples)
num_oranges = 0
print("Number of oranges:", num_oranges)
JavaScript Solution:
function start() {
var numApples = 20;
println("Number of Apples: " + numApples);
var numOranges = 15;
println("Number of Oranges: " + numOranges);
// Reprint apples
println("Number of Apples: " + numApples);
// Set oranges to 0
numOranges = 0;
println("Number of Oranges: " + numOranges);
}
Key Concepts for 3.4.6:
1. Declare a variable: num_apples = 20 2. Print a variable as text: print("Number of
apples: " + str(num_apples)) str() converts the integer to a string so it can be
concatenated with " + " 3. Reassign a variable: num_oranges = 0 (updates the value) 4.
You must use the variable in the print — NOT the raw number!
Exercise 3.5.4: Obi-Wan Says
Task: Ask the user their name, how many droids they'd like to meet, and how many Wookiees. Print a message
using all three inputs.
Python Solution:
# Place your code here
name = input("What is your name? ")
droids = int(input("How many droids would you like to meet? "))
wookies = int(input("How many Wookiees would you like to meet? "))
print("Hi " + name + ", you want to meet " + str(droids) + " droids and " + str(wookies) + " Wookiees.")
Expected Output:
Hi Alex, you want to meet 3 droids and 2 Wookiees.
JavaScript Solution:
function start() {
var name = readLine("What is your name? ");
var droids = readInt("How many droids would you like to meet? ");
var wookies = readInt("How many Wookiees would you like to meet? ");
println("Hi " + name + " you want to meet " + droids + " droids and " + wookies + " Wookiees.");
}
Tip: Use int(input()) for numbers and just input() for text. In Python, always wrap int variables in str() when concatenating
with +. In JavaScript, readInt() and readLine() handle user input.
Exercise 3.6.7: Sporting Goods Shop
Task: Frisbees cost $15 each. Ask the user how many frisbees they want to buy and print the total cost.
Python Solution:
# Enter your code here
COST_OF_FRISBEES = 15
amount = int(input("How many Frisbees would you like to buy? "))
cost = COST_OF_FRISBEES * amount
print("The cost for " + str(amount) + " frisbees is $" + str(cost) + " dollars.")
Expected Output:
How many Frisbees would you like to buy? 3
The cost for 3 frisbees is $45 dollars.
JavaScript Solution:
function start() {
var COST_OF_FRISBEE = 15;
var amount = readInt("How many would you like? ");
var sum = COST_OF_FRISBEE * amount;
println("The cost for " + amount + " frisbees is $" + sum + " dollars.");
/ 7
Related