Pair work Ch 4.1
- Due Feb 16, 2022 by 9:50am
- Points 1
- Available after Feb 16, 2022 at 9am
Problem 1
This is adapted from from problem 4.1.16 in the book.
Python also allows us to pass function names as parameters. So we can generalize the function plotSine
function in the program ch4_sine.py
Links to an external site. to plot any function we want. Modify ch4_sine.py by adding a function
plot(tortoise, n, f)
where f
is the name of an arbitrary function that takes a single numerical argument and returns a number. Inside the for loop in the plot function, we can apply the function f
to the index variable x
with
tortoise.goto(x,f(x))
To call the plot function, we need to define one or more functions to pass in as arguments. For example, to plot x2, we can define
def square(x):
return x * x
and then call plot with
plot(george, 20, square)
Or, to plot an elongated sin x, we could define
def sin(x):
return 10 * math.sin(x)
and then call plot with
plot(george, 20, sin)
After you create your new version of plot, also create at least one new function to pass into plot for the parameter f
. Depending on the functions you pass in, you may need to adjust the window coordinate system with setworldcoordinates
.
Problem 2
This is based on problem 4.1.33 of the book. Suppose you have 1,000 to invest and need to decide between two savings accounts. The first account pays interest at an annual rate of 1% and is compounded daily—every day, the interest accrued is incremented by (1/365)% of the principal plus the interest previously accrued. The second account pays interest at an annual rate of 1.25% but is compounded monthly. Write a function
interest(originalAmount, rate, periods)
that computes the interest earned in one year on originalAmount
dollars in an account that pays the given annual interest rate, compounded over the given number of periods. Assume the interest rate is given as a percentage, not a fraction (i.e., 1.25 vs. 0.0125). Use the function to answer the original question. Do no use the formula we used in the previous class on interest; use a for loop.