Repository navigation
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:.
- What You Need to Do
- How the File Is Organized
- Why the File Uses This Structure
- The Module Docstring
- Importing
date - The
CURRENT_YEARConstant - Documentation and Comments
- The
main()Function - The Main Guard
- How Python Runs the File
- How the Structure Supports the SDLC
- Learn More
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.
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.
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 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.
The starter file contains:
from datetime import dateYou 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 starter file contains:
CURRENT_YEAR = date.today().yearThis expression works from left to right:
-
date.today()provides the current local date. -
.yearselects the year from that date. -
CURRENT_YEAR =assigns that integer year to the nameCURRENT_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.
The starter file demonstrates several ways to document Python code.
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.
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.
An inline comment appears after Python code on the same line:
CURRENT_YEAR = date.today().year # Get current year from system as integerAn 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.
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 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.
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 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.pydirectly,__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.
When you run name_age.py directly, Python processes the provided structure in this order:
- Python reads the module docstring.
- The import makes
dateavailable. - The constant receives the current year.
- Python defines
main()but does not run its statements yet. - The main guard's condition is true because the file is running directly.
-
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.
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.
These references provide additional background. You do not need to study them to complete the assignment.