2.2 Code Practice Question 1 Python Answer

7 min read

Understanding the logic behind introductory programming exercises is far more valuable than simply copying a solution. In practice, in most standard Python curricula—such as CodeHS, Edhesive, Coursera’s Python for Everybody, or standard AP Computer Science Principles courses—Section 2. 2 typically focuses on Variables, Data Types, and User Input Easy to understand, harder to ignore..

While the exact wording of "Question 1" varies by platform, the core objective is almost always the same: write a program that asks the user for input, stores that input in a variable, and prints a response using that variable.

Below is a complete walkthrough to solving the standard "Question 1" pattern for this module, including the code, a line-by-line breakdown, common errors, and the computer science concepts you are actually being tested on Simple, but easy to overlook..


The Standard Scenario: "The Greeting Program"

In 90% of introductory Python courses, Module 2.2 Question 1 asks you to build a variation of this program:

Prompt: Write a program that asks the user for their name and then prints a greeting message that includes their name.

The Correct Python Answer

Here is the clean, standard Python 3 solution accepted by almost all auto-graders:

# 1. Ask the user for their name and store it in a variable
name = input("What is your name? ")

# 2. Print the greeting using concatenation or an f-string
print("Hello, " + name + "! Nice to meet you.")

# Alternative modern approach (Python 3.6+):
# print(f"Hello, {name}! Nice to meet you.")

Line-by-Line Code Explanation

If you are submitting this for a grade, you likely need to understand why this works. Here is the breakdown:

1. The input() Function

name = input("What is your name? ")
  • input("..."): This built-in function pauses the program and waits for the user to type something on the keyboard and press Enter.
  • The String Argument: The text inside the parentheses ("What is your name? ") is the prompt. It is displayed to the user so they know what to type. Pro tip: Always add a space at the end of your prompt string so the user's cursor doesn't sit right against the question mark.
  • Return Value: input() always returns a String (str). Even if the user types 42, Python treats it as the text "42", not the number 42.
  • Assignment (=): The = operator takes the string the user typed and assigns it to the variable name. Now, the computer "remembers" the input under that label.

2. The print() Function & String Concatenation

print("Hello, " + name + "! Nice to meet you.")
  • Concatenation (+): In Python, the + operator joins (concatenates) strings together. You cannot "add" a string and an integer, but since input() returns a string and name holds a string, this works perfectly.
  • Commas vs. Plus Signs: You might see tutorials use print("Hello,", name, "!"). While this works, it automatically inserts spaces between items (output: Hello, Alice !). Using + gives you total control over spacing (output: Hello, Alice!), which is often required by strict auto-graders.

3. The Modern Alternative: f-strings (Formatted String Literals)

print(f"Hello, {name}! Nice to meet you.")
  • Introduced in Python 3.6, the f before the quotation marks allows you to embed variables directly inside the string using curly braces {}.
  • This is generally considered best practice in modern Python development because it is faster and more readable. On the flip side, some older curriculum auto-graders specifically check for concatenation (+). If your grader fails on the f-string, switch to the concatenation version.

Common Variations of 2.2 Question 1

Curriculums often tweak the requirements slightly. Here are the three most common variations and how to handle them.

Variation A: Multiple Inputs (Name + Age)

Prompt: Ask for the user's name and age. Print: "Hello [Name], you are [Age] years old."

name = input("Enter your name: ")
age = input("Enter your age: ")  # Remember: this is a STRING, not an int!

# Using concatenation (safest for graders)
print("Hello " + name + ", you are " + age + " years old.")

Critical Note: Do not wrap input() in int() here unless the next question asks you to do math with the age. If you convert to int, you cannot concatenate it with strings in the print statement without converting it back to str().

Variation B: Numerical Calculation (The "Mad Libs" Math Problem)

Prompt: Ask for two numbers. Print the sum.

# input() returns strings, so we MUST convert to int or float
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

# Calculate
total = num1 + num2

# Print: Must convert total back to string for concatenation, 
# or use commas/f-strings.
print("The sum is " + str(total)) 
# OR
print(f"The sum is {total}")

Variation C: Specific Formatting Requirements

Some auto-graders (especially CodeHS or Edhesive) are notoriously picky about exact punctuation and capitalization.

  • Requirement: Hello, Dave! (Comma, space, exclamation)
  • Your Code: print("Hello, " + name + "!")
  • Failure Case: print("Hello " + name + "!") (Missing comma) or print("Hello, " + name + ".") (Wrong punctuation).

Always match the example output in the problem description character-for-character.


The Computer Science Concepts Being Tested

This exercise isn't just about syntax; it tests four foundational pillars of programming. Understanding these will help you on the exam, not just the homework.

1. Variables as "Labeled Boxes"

A variable (name) is a reserved memory location with a label. The assignment operator (=) means "store the value on the right into the box on the left." It is not a mathematical equality statement It's one of those things that adds up..

2. Data Types Matter (Type System)

Python is dynamically typed but strongly typed.

  • input() $\rightarrow$ String (str)
  • "5" + "5" $\rightarrow$ "55" (String Concatenation)
  • 5 + 5 $\rightarrow$ 10 (Integer Addition)
  • The Trap: Trying to do "Hello " + 5 raises a TypeError. You must explicitly convert types

using functions like str(), int(), and float(). This is called type casting or type conversion. For example:

  • int("5") converts the string "5" into the integer 5
  • str(5) converts the integer 5 into the string "5"
  • float("3.14") converts a string into a decimal number

Python will not guess your intention. If you try to add a string and an integer without converting first, the program crashes with a TypeError Simple, but easy to overlook..

3. Input and Output (I/O)

input() is how your program receives data from the outside world. It pauses execution and waits for the user to press Enter, and everything it captures is a string. print() is how your program sends information back to the screen. Together, they form the basic input-process-output cycle that underlies nearly every program you will write.

4. String Concatenation vs. Formatting

There are multiple ways to combine text and variables:


String concatenation joins strings together by placing them in order. You can combine text and variables, but every piece must be a string.

print("Hello, " + name + "!")

A more modern and readable approach is an f-string, which allows you to embed expressions directly inside the string using curly braces Most people skip this — try not to. Nothing fancy..

print(f"Hello, {name}!")

For numbers, you might first calculate a result and then display it cleanly Worth keeping that in mind..

print(f"The sum is {total}")

Choosing between concatenation and f-strings often comes down to readability and context. Concatenation is useful when you have simple pieces or when you are building strings dynamically. F-strings are usually preferred in modern Python because they are concise and evaluate expressions clearly.

Beyond these syntax choices, the underlying lesson remains important: programs handle data through variables, respect data types, accept input, produce output, and format results. Each concept reinforces the next, and small mistakes in any of them can change or crash a program.

With practice, these building blocks become second nature. It requires understanding how data is stored, how values are converted, how input and output work, and how to present information effectively. Pulling it all together, writing clear Python code depends on more than knowing a single command. By mastering these fundamentals, you can move from simple exercises to more complex programs with confidence.

Fresh Out

Just Wrapped Up

Worth the Next Click

Explore a Little More

Thank you for reading about 2.2 Code Practice Question 1 Python Answer. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home