Skip to content

IPP-DEV-02-002: Simple Numeric Calculations #59

@labbenchstudios

Description

@labbenchstudios

Description

  • Using instructions from the in-class lecture, create a module to test some simple calculations using integers (int) and floats (float).

Review the README

  • Please see README.md for further information on, and use of, this content.
  • License for embedded documentation and source codes: IPP-DOC-LIC

Estimated effort may vary greatly

  • The estimated level of effort for this exercise shown in the 'Estimate' section below is a very rough approximation. The actual level of effort may vary greatly depending on your development and test environment, experience with the requisite technologies, and many other factors.

Actions

Step 1: Make Sure Your System is Setup for Python and This Course

Step 2: REMINDER: Make Sure PYTHONPATH is Set Correctly

Whether running Python tests within your IDE or from the command line, you must set the PYTHONPATH environment variable in every execution environment (e.g., every terminal you launch) when attempting to run any of your scripts and their tests or the IPP test app from the command line. The IPP source and test paths will be as follows:

  • {your IPP source code path}
  • {your IPP source code path}/tests

See IPP-DEV-01-001 for details.

Step 3: Create your own module to test simple numeric calculations

  • Using your IDE, or from within a terminal, create a new Python file - a module - named SimpleNumericCalcs.py within the labmodule02 path (or package)
  • Open the module, and create the indicated test cases:

Test 1 (addition / subtraction)

  • Create a comment at the beginning of the file:
    • For example:
    • # Test 1: simple int calcs
  • Declare the picked_apples variable as an integer. Use a value between 20 and 50
  • Declare the bagged_apples variable as an integer. Use a value between 5 and 10
  • Declare and calculate available_apples. This will be the sum of picked_apples and bagged_apples
  • Declare consumed_apples as an integer. Use a value between 1 and 10
  • Calculate remaining_apples as available_apples less consumed_apples
  • Print all the variables to the console. You can use multiple print statements if you'd like.
    • For example:
       print(f"Apples collected. Picked: {picked_apples}; Bagged: {bagged_apples}; Available: available_apples}")
       print(f"After eating some apples. Consumed: {consumed_apples}; Remaining: {remaining_apples}")

Test 2 (product)

  • Within the same module, add a blank newline after the previous test, and create another comment:
    • For example:
    • # Test 2: simple product calcs
  • Declare the shopping_trips variable as an integer. Use a value between 2 and 4
  • Declare the apples_per_trip variable as an integer. Use a value between 1 and 5
  • Calculate purchased_apples as shopping_trips * apples_per_trip
  • Calculate total_apples as the sum of remaining_apples and purchased_apples
  • Print all the variables to the console. You can use multiple print statements if you'd like.

Test 3 (remainders)

  • Within the same module, add a blank newline after the previous test, and create another comment:
    • For example:
    • # Test 3: remainders
  • Declare the days_per_week variable as the integer value of 7
  • Calculate daily_apples_for_week as an integer, passing in the parameter of total_apples / days_per_week
    • e.g., int(total_apples / days_per_week)
  • Calculate left_over_apples_mod as the modulo of total_apples and days_per_week
    • e.g., total_apples % days_per_week
  • Calculate left_over_apples_sub as total_apples the product of daily_apples_for_week and days_per_week
    • e.g., total_apples - (daily_apples_for_week * days_per_week)
  • Print the apple daily consumption variables on one line
    • Be sure to include total_apples and daily_apples_for_week
  • Print the leftover variables on another line
    • Be sure to include left_over_apples_mod and left_over_apples_sub

Estimate

  • Small

Tests

  • From within your IDE

    • Right click on your newly created module SimpleNumericCalcs.py and click your IDE's run icon
    • You should see output similar to that discussed in class
  • From the command line

    • Open a terminal and cd to your IPP_HOME path
    • Start your virtual environment (if not already running)
    • Be sure your PYTHONPATH is set correctly
    • Run the module
      • python ./labmodule02/SimpleNumericCalcs.py
    • You should see output similar to that discussed in class

Sample output (yours may differ slightly)

Apples collected. Picked: 20; Bagged: 10; Available: 30
After eating some apples. Consumed: 5; Remaining: 25
Apple shopping. Shopping Trips: 2; Apples per Trip: 3; Purchased: 6
Total Apples: 31
Daily apple consumption. Total: 31; Apples per day: 4
Leftovers. Using modulus: 3; Using subtraction: 3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Lab Module 02 - Syntax

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions