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], whereXis the chapter andYis the section number.Available at:
https://runestone.academy/ns/books/published/thinkcspy/index.html?mode=browsingAccessed: 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 exceptinitials.py).Procedure:
Copy these files into a new folder.
For each script, create a new script that uses a function to calculate its output instead of performing calculations at the global scope.
Function Naming: The function in each script should be named according to the file name (e.g.,
areaOfRectangle.pyshould have anareaOfRectangle()function).Parameters: Functions should take input values as parameters.
Citations: Include citations inside these new functions.
main()Function: Use amain()function, similar to Runestone section 6.8.Entry Point: Use the
if __name__ == "__main__":version as seen in Runestone activity 6.8.2.Call
main(): Callmain()inside theif __name__ == "__main__":statement.Input Collection: Collect user input inside the
main()function.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 inmain()(e.g.,answer = areaOfRectangle(width, height)).Print Results: Print the calculation results (e.g.,
print("The answer is:", answer)).
Example Reference: Refer to
myFunctionExample.pyfor 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, andc.Function:
randomProduct(a, b, c).Takes
a,b, andcas input parameters.Uses the
randrange()function to calculate the product ofarandom numbers.The random numbers should be in the range between
bthroughc + 1(non-inclusive forrandrange(), meaningbtocinclusive).Example: If , , , product of 5 random numbers between 1 and 6 inclusive (argument for
randrange()would be because ).Example: If , , , a possible calculation could be (three random numbers from the range).
Hint: A
for loopwill be involved.Hint: Import the
randommodule.Assumption: User provides proper input, meaning b <= c is always true.
main()Function Structure: Follow standardmain()andif __name__ == "__main__":format.Input Collection: Collect
a,b,cinmain().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