Chapter 2: Input, Processing, and Output - Program Development & Python Basics
Program Development Cycle
Steps: Talk to the user, design the program, write the code, correct syntax errors, test the program, correct logic errors.
Talk to the User: Paramount for project success; ensures user needs are met. Often involves different roles (e.g., data analyst, developer, marketer).
Design the Program: Uses tools like flowcharts (e.g., Lucidchart) and pseudocode.
Write the Code: Requires a clear understanding of user requirements; otherwise, "Garbage In, Garbage Out" (GIGO).
Correct Syntax Errors: AI tools can assist, but do not assume AI is always perfect or provides the most efficient solution.
Test the Program: Verify program output with provided test data. A running program is not necessarily a correct one.
Correct Logic Errors: Program runs without crashing but produces incorrect results. Examples include using a wrong variable, incorrect indentation (critical in Python loops/conditionals), or incorrect order of mathematical operations (missing parentheses).
Python Best Practices
Comments: Crucial for code clarity and maintainability.
Provide an initial comment explaining the program's overall purpose.
List and initialize variables, explicitly defining their data types (e.g.,
for float,for integer).
Avoid Magic Numbers: Do not embed unexplained numerical constants directly into calculations (e.g.,
). Instead, assign them to descriptive variables (e.g.,).User Input: Prompt the user to enter values.
Use
orto ensure input is stored as a numeric data type (float or integer, respectively) and not as a string.
Output Formatting
Two Decimal Places: Use the
function withinto specify decimal precision.Syntax:
.The string
\".2f\"formats the number to a fixed-point notation with exactly two digits after the decimal point.
Homework Assignment (Due Monday)
Task: Develop a program to calculate a company's annual profit, which is typically
of total sales.Deliverables (compiled onto a single Word document):
Flowchart (created using Lucidchart).
Pseudocode.
Actual Python Code.
Program Output (using specified test data).
Specific Requirements:
NO Magic Numbers: Define the
profit rate as a named variable.User Input: Prompt the user to enter the projected amount of total sales.
Output Formatting: Format the calculated profit to two decimal places.
Test Data: Use total sales of
.Output Content: Display ONLY the calculated profit. Do NOT add the profit back to the sales total for the final display. Adjust output strings to clearly state "The profit is:".
Binary/Base 10 Conversion (Reference, related to previous homework)
Binary String to Base 10 Integer:
Base 10 Integer to Binary String:
(Output string includes anprefix, standing for Output Binary).