Lab #3: Introduction to Computer Programming

Lab #3: Introduction to Computer Programming

Lab Objective
  • The primary purpose of this lab is to familiarize students with:

    • Creating functions.

    • Assigning function output to a variable when the function returns.

    • Gaining more practice using loops.

    • Further practice with the 'turtle' library.

General Instructions and Deliverables
  • Tasks can be completed in any order.

  • See the 'Grading Items' section for point distribution.

  • Citation Guidelines:

    • Exercises adapted from "How to Think Like a Computer Scientist: Interactive Edition" (Jeffrey Elkner et al.) are indicated by [thinkcspy X.Y], where X is the chapter and Y is the section number.

      • Available at: https://runestone.academy/ns/books/published/thinkcspy/index.html?mode=browsing

      • Accessed: 2-2-2023

    • Original exercises by the instructor are indicated by [MH].

    • Ensure all new functions include appropriate citations.

Lecture Notes/Supplemental Readings (Required for Engineering Notebook)
  • These notes should be completed before the lab period.

  • If lecture notes were taken in class, Runestone chapter notes are not required as the content overlaps.

  • Required Materials:

    • Runestone chapter 6.

      • Note: Completing exercises at the end of Runestone chapters is optional but helpful long-term.

    • Debugging.pptx (Available: 'Files' -> 'SupplimentalSlidesand_Files' folder on Canvas).

    • Debugging_in_VS_Code.pptx (Available: 'Files' -> 'SupplimentalSlidesand_Files' folder on Canvas).

    • "What Does if name == "main" Do in Python?" - by: Martin Breuss.

      • Available: https://realpython.com/if-name-main-python/

Code Conversion pt. 1 [MH]
  • Objective: Convert existing scripts from Lab Week 3 into functions.

  • Files to Convert: areaOfRectangle.py, rectanglePerimeter.py, areaOfCircle.py, circleCircumference.py, distanceSpeedTime.py, velocityAccelerationTime.py, calculateVoltage.py, calculateResistance.py, calculateCurrent.py, annualPercentageRate.py, compoundAmount.py (all except initials.py).

  • Procedure:

    1. Copy these files into a new folder.

    2. For each script, create a new script that uses a function to calculate its output instead of performing calculations at the global scope.

    3. Function Naming: The function in each script should be named according to the file name (e.g., areaOfRectangle.py should have an areaOfRectangle() function).

    4. Parameters: Functions should take input values as parameters.

    5. Citations: Include citations inside these new functions.

    6. main() Function: Use a main() function, similar to Runestone section 6.8.

    7. Entry Point: Use the if __name__ == "__main__": version as seen in Runestone activity 6.8.2.

    8. Call main(): Call main() inside the if __name__ == "__main__": statement.

    9. Input Collection: Collect user input inside the main() function.

    10. Function Call & Return: Call the new function (e.g., areaOfRectangle(width, height)) and pass input values. Collect the return value in a new variable back in main() (e.g., answer = areaOfRectangle(width, height)).

    11. Print Results: Print the calculation results (e.g., print("The answer is:", answer)).

  • Example Reference: Refer to myFunctionExample.py for structural details.

  • Saving: Save updated code to the original file names. Each file must include your name, code creation date, lab number, and a brief description.

randomProduct.py [thinkcspy 5.7]
  • Objective: Create a function to calculate the product of 'a' random numbers within a given range.

  • Inputs: Three integers from the user: a, b, and c.

  • Function: randomProduct(a, b, c).

    • Takes a, b, and c as input parameters.

    • Uses the randrange() function to calculate the product of a random numbers.

    • The random numbers should be in the range between b through c + 1 (non-inclusive for randrange(), meaning b to c inclusive).

      • Example: If a=5a = 5, b=1b = 1, c=6c = 6, product of 5 random numbers between 1 and 6 inclusive (argument for randrange() would be 77 because 6+1=76 + 1 = 7).

      • Example: If a=3a = 3, b=2b = 2, c=5c = 5, a possible calculation could be 3543 * 5 * 4 (three random numbers from the range).

    • Hint: A for loop will be involved.

    • Hint: Import the random module.

    • Assumption: User provides proper input, meaning b <= c is always true.

  • main() Function Structure: Follow standard main() and if __name__ == "__main__": format.

  • Input Collection: Collect a, b, c in main().

  • Function Call & Return: Call randomProduct(a, b, c) and store the return value (e.g., answer = randomProduct(a, b, c)).

  • Print Results: Print the answer.

  • Example Reference: Refer to myFunctionExample.py.

  • Saving: Save code to randomProduct.py, including name, date, lab number, and description.

sqrtIter.py [thinkcspy 6.13]
  • Objective: Implement an iterative method to calculate the square root of a number.

  • Review Source: https://www.cuemath.com/algebra/square-root-of-2/ (accessed 2-2-2023).

    • Specifically, the