Skip to content

Understanding the Name and Age Starter File

mike-snhu edited this page Sep 13, 2026 · 1 revision

Understanding the Name and Age Starter File

The Module Two name_age.py starter file introduces the structure of a complete Python module while you practice basic input, variables, arithmetic, type conversion, and formatted output.

Important

This page provides optional background information. It does not add any assignment requirements. To complete the assignment, follow the Construct phase instructions and change only lines marked TODO:.

Table of Contents

What You Need to Do

You are not expected to write or fully understand all the starter code yet.

For this assignment:

  • Complete only lines marked TODO:.
  • Leave the import, constant, main() definition, main guard, and other provided code unchanged.
  • Keep each Python statement that you write inside main() indented four spaces.
  • Use the required Construct phase instructions for the steps you must complete.

How the File Is Organized

The starter file uses this order:

Part Purpose Your responsibility
Module docstring Documents the program's purpose, input, process, output, and use Complete its TODO: lines in your own words.
Imports Makes code from another module available Leave unchanged.
Constants Gives a name to a value the program treats as unchanged Leave unchanged.
Main function Contains the program's main sequence of statements Replace only its four code TODO comments.
Main guard Starts main() when the file is run directly Leave unchanged.
References Identifies outside sources that influenced the work Replace or delete its TODO comments as directed.

This structure separates the program's documentation, setup, main work, starting point, and references. The section-header comments make those parts easier to recognize while you are learning.

Why the File Uses This Structure

A very small Python program can place every statement at the left edge of the file. The same program can also be organized inside a function. The starter file uses a function and a main guard so you can begin recognizing a structure that works well as programs and repositories become larger.

The structure also separates two levels of learning:

  • Your current task: Write four basic Python statements using concepts covered in Modules One and Two.
  • Your preview of later concepts: Recognize imports, constants, functions, indentation blocks, and a main guard without having to create them yet.

The provided structure is part of the starter file, not an additional coding problem for you to solve.

The Module Docstring

The triple-quoted text at the beginning of the file is a module documentation string, usually called a module docstring. It documents the entire Python file.

The starter docstring begins with a one-line summary and then includes these instructional sections:

  • Input — data the program needs, including its type and source
  • Process — how the program changes input into output
  • Output — what the program produces and where it goes
  • Typical usage example — an example showing the program being used

The Input → Process → Output organization connects the source file to the IPO work in your Software Development Worksheet. It is an IT 140 learning convention, not a required format for every Python docstring.

A docstring is a string used as documentation. It is not technically a Python comment. Python tools can read a module's docstring as information about that module.

Importing date

The starter file contains:

from datetime import date

You learned in zyBooks that a module contains Python code that another program can import and use. datetime is a module in the Python Standard Library. A standard library is a collection of modules included with Python.

The datetime module defines a class named date. The import makes the name date directly available in this file. That allows the next line to use:

date.today()

date.today() provides the computer's current local date. You do not need to understand classes or methods to use the provided starter code in this assignment.

The CURRENT_YEAR Constant

The starter file contains:

CURRENT_YEAR = date.today().year

This expression works from left to right:

  1. date.today() provides the current local date.
  2. .year selects the year from that date.
  3. CURRENT_YEAR = assigns that integer year to the name CURRENT_YEAR.

A constant is a named value that a program intends to leave unchanged while the program runs. Python does not prevent a constant from being changed. Instead, programmers use an uppercase name such as CURRENT_YEAR to communicate that the value should remain unchanged.

The constant is assigned before main() runs. Your code can therefore use CURRENT_YEAR inside main() when it performs the required calculation.

This current year is an input to the program even though the user does not type it. The program obtains it internally from the computer's date.

Documentation and Comments

The starter file demonstrates several ways to document Python code.

Docstrings

A docstring is a string used to document a module, function, class, or method. The starter file contains:

  • A multi-line module docstring at the beginning of the file
  • A one-line docstring inside main()

The module docstring includes TODO lines that you must complete. The main() docstring is already complete and should not be changed.

Block Comments

A block comment appears on its own line and describes code that follows it:

# Calculate user's approximate birth year.

The # TODO: comments inside main() are also block comments. In this file, TODO marks a task that still needs to be completed. You replace each of those comments with a Python statement.

Inline Comments

An inline comment appears after Python code on the same line:

CURRENT_YEAR = date.today().year  # Get current year from system as integer

An inline comment should add information that helps the reader. In a professional program, this example comment might be unnecessary because the code and name are already descriptive. It is included in the starter file to support your learning.

Instructional Section Headers

The starter file includes comments such as:

# === Imports ===
# === Constants ===
# === Main Function ===

These headers are instructional scaffolding. They label each part so you can recognize the organization of your first structured Python module.

Small professional Python files usually do not need large section-header comments. They more often use a conventional order, descriptive names, helpful comments, and blank lines. Python style guidance normally uses two blank lines around top-level function definitions, which is why you see extra blank lines before and after main().

The main() Function

The starter file defines this function:

def main() -> None:
    """Run the name-age program."""

Each part has a purpose:

Code Meaning
def Begins a function definition.
main Gives the function its name.
() Shows where function inputs called parameters would be listed. This function has no parameters.
-> None Indicates that the function does not return a value.
: Begins the indented function body.
"""Run the name-age program.""" Documents the function's purpose.

The name main is a common convention for the function that controls a program's main sequence. Python does not automatically run a function merely because it is named main. The main guard described in the next section calls it.

Why the Four-Space Indentation Matters

Python uses indentation to group statements into a block. The statements indented beneath def main() belong to the function:

def main() -> None:
    """Run the name-age program."""

    # A statement written here belongs to main().

The standard indentation level is four spaces. If you move one of your statements to the left edge of the file, it will no longer be part of main(). If you add an incorrect number of spaces, Python may report an indentation error.

The Main Guard

The starter file ends its program code with:

if __name__ == "__main__":
    main()

This pattern is called the main guard.

Python gives the special variable __name__ a value when it loads a file:

  • When you run name_age.py directly, __name__ has the value "__main__".
  • When another Python file imports name_age.py, __name__ has the module's name instead.

The if statement checks whether the file is being run directly. If it is, the indented main() statement calls the function and starts the program. If the file is imported, Python defines main() but does not automatically run it.

This separation becomes useful when code is reused or tested. For this assignment, the important rule is simple: leave both lines of the main guard unchanged.

How Python Runs the File

When you run name_age.py directly, Python processes the provided structure in this order:

  1. Python reads the module docstring.
  2. The import makes date available.
  3. The constant receives the current year.
  4. Python defines main() but does not run its statements yet.
  5. The main guard's condition is true because the file is running directly.
  6. main() is called, so Python runs its indented statements in order.

This explains why your four Python statements belong inside main() and why the main guard must remain at the end of the provided program code.

How the Structure Supports the SDLC

The repository separates different kinds of work into folders that match the simplified software development life cycle (SDLC):

  • analysis/ contains the requirements that define what the program must do.
  • design/ contains the design that plans how the program will work.
  • src/ contains the source code you construct.
  • tests/ contains acceptance tests used to check the completed source code.

Within src/name_age.py, the module docstring records the program's purpose and IPO information, main() contains the constructed solution, and the main guard provides a clear starting point when the source file is run during Construct and Test.

The advanced structure therefore supports the repository workflow, while the TODO lines keep your required programming work limited to concepts introduced by this point in the course.

Learn More

These references provide additional background. You do not need to study them to complete the assignment.

Clone this wiki locally