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., price=0.0price = 0.0 for float, price=0price = 0 for integer).

  • Avoid Magic Numbers: Do not embed unexplained numerical constants directly into calculations (e.g., taxamount=price0.1tax_amount = price * 0.1). Instead, assign them to descriptive variables (e.g., TAXPERCENT=0.1TAX_PERCENT = 0.1).

  • User Input: Prompt the user to enter values.

    • Use variable=float(input(Eˊntertheprompt:)ˊ)variable = float(input(\'Enter the prompt:\')) or variable=int(input(Eˊntertheprompt:)ˊ)variable = int(input(\'Enter the prompt:\')) to 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 format()format() function within print()print() to specify decimal precision.

    • Syntax: print(Tˊheamountis:,ˊformat(variable,.¨2f)¨)print(\'The amount is: \', format(variable, \".2f\")).

    • 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 23%23\% of total sales.

  • Deliverables (compiled onto a single Word document):

    1. Flowchart (created using Lucidchart).

    2. Pseudocode.

    3. Actual Python Code.

    4. Program Output (using specified test data).

  • Specific Requirements:

    • NO Magic Numbers: Define the 23%23\% 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 257.68257.68.

    • 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: x=int(1ˊ101,ˊ2)x = int(\'1101\', 2)

  • Base 10 Integer to Binary String: z=bin(43)z = bin(43) (Output string includes an OBOB prefix, standing for Output Binary).