The goal of this assignment is to allow you to practice utilizing
iteration in the Python programming language. To record your
solutions and answers, create a new Jupyter Notebook titled
Notebook03.ipynb
and use this notebook to complete the following
activities and answer the corresponding questions.
Make sure you label your activities appropriately. That is for Activity 1, have a header cell that is titled Activity 1. Likewise, use the Markdown cells to answer the questions (include the questions above the answer).
This Notebook assignment is due midnight Thursday, September 15, 2016 and is to be done individually.
For this activity you are to create a guessing game that:
Generates a random number as the target
.
Asks the user to guess
the random number.
If the guess
is less than the target
, the program should tell the
user to guess Higher
.
If the guess is greater than the
target, the program should tell the
user to guess
Lower`
Otherwise, the program should say Yay, you found
the target
.
The program should limit the user to 3
attempts. After the third
attempt, the program should stop prompting the user and say, Sorry, but
the number was
the target
.
A transcript of the program should go as follows:
Guess? 1
Higher!
Guess? 3
Higher!
Guess? 5
Lower!
Sorry, but the number was 4
Here is another possible transcript:
Guess? 5
Lower!
Guess? 3
Yay, you found 3
Use random.randint to generate a target
between 0
and 9
.
Use a counter to keep track of the number of attempts.
Describe in your own words how the the guessing game works. What information are you keeping track of? How do you know when the game is over?
Did you use continue
or break
? If so, come up with a solution that
does not use them. If you did not use them, come up with a solution that
does use either of them (does not need to be both).
Which solution do you prefer and why?
For this activity, you are to implement the Monte Carlo simulation of Pi.
The Monte Carlo method
is a way of finding approximate solutions to problems that cannot be
precisely solved. A common example of such a problem is determining the
value of pi
.
To compute the value of pi
, we can do the
following:
Let's consider the problem of estimating Pi by utilizing the Monte Carlo method. Suppose you have a circle inscribed in a square (as in the figure). The experiment simply consists of throwing darts on this figure completely at random (meaning that every point on the dartboard has an equal chance of being hit by the dart). How can we use this experiment to estimate Pi?
The answer lies discovering the relationship between the geometry of the figure and the statistical outcome of throwing the darts.
Let's first look at the geometry of the figure. Let's assume the radius of the circle is R, then the Area of the circle =
Pi*R
2 and the Area of the square =4*R
2.Now if we divide the area of the circle by the area of the square we get
Pi/4
.But, how do we estimate Pi by simulation? In the simulation, you keep throwing darts at random onto the dartboard. All of the darts fall within the square, but not all of them fall within the circle. Here's the key. If you throw darts completely at random, this experiment estimate the ratio of the area of the circle to the area of the square, by counting the number of darts in each.
Our study of the geometry tells us this ratio is
Pi/4
. So, now we can estimate Pi as
Pi = 4 x (Number of Darts in Circle) / (Number of Darts in Square)
The common approach to implementing this simulation involves computing a
pair of x
and y
coordinates which range between [-1, 1]
in each
iteration and checking to see if that point is within a circle with a
center at the origin and a radius of 1
. If the point is within a circle,
increment the counter that tracks the number of darts in a circle. This
approach is shown in the image below.
Alternatively, you can generate x
and y
coordinates with a range of
[0, 1]
and check if the hypotenuse of this point with respect to the
origin is less than 1
. The advantage of this method is that we can
simply use random.random()
to compute two numbers between [0, 1]
and
then math.hypot(x, y)
to compute the hypotenuse of this coordinate with
respect to the origin and x
-axis. This approach is shown in the image
below.
For your experiments, throw at least 100
darts. After your simulation,
display the computed value of pi
using the formula above.
Use random.random to generate x
and y
coordinates.
Use math.hypot to check if the coordinate is in the radius of the circle.
After completing the activity above, answer the following questions:
Describe in your own words how the simulation works. What are the steps you are computing? What are you keeping track of in each iteration?
How does the number of darts thrown affect the accuracy of the estimate? Provide evidence for your answer.
For this activity, you are to implement a mortgage calculator that given the principal, interest rate, and desired monthly payment, the program generates an amortization table and displays how long it took to pay off the mortgage (in years and months) and the total amount of payments over that time.
In the recent housing crisis, many unsuspecting homebuyers got themselves into complicated mortgages that they couldn't afford, and perhaps didn't understand. Most mortgage companies go out of their way to hide from the buyer what the actual cost of purchasing a house is, so you should be able to calculate it for yourself.
A mortgage is quite simple. The bank loans you a certain amount of money (the principal) to purchase a house at a certain interest rate. Every month, you must make a payment to reduce the balance. In addition, the bank changes interest by computing one twelfth of the interest rate times the remaining balance. (Unfortunately, the interest gets charged before your payment is applied.)
For example, suppose that you borrow $100,000
to purchase a home at 5%
yearly interest. You agree to pay $500
per month until the mortgage is
paid off. In the first month, the interest increases the balance by
$416.67
, then your payment reduces it by $500
, for a remaining balance
of $99,916.67
. The first payment only reduced the principal by $83.33
!
(This is going to take a while.)
In the second month, the interest charge is $416.31
, and the remaining
balance is $99832.99
. And so on. If you keep computing like this, you get
what is known as an amortization table that shows every payment until
the mortgage is paid off:
Month Payment Interest Balance
1 $500.00 $416.67 $ 99916.67
2 $500.00 $416.32 $ 99832.99
...
430 $500.00 $ 3.97 $ 456.94
431 $458.84 $ 1.90 $ 0.00
In computing this table, you should be able to report how long it would take to pay off the mortgage and what the total amount paid was:
You paid a total of $215458.84 over 35 years and 11 months.
Start with some simple examples that complete with a few payments, then verify your results by hand.
For dollar values, only display two digits of precision after the decimal point by using string formatting.
The final payment will almost certainly be smaller than all the others, so be careful to check for that case so you don't end up with a negative balance.
If the desired payment is too small, balance will go up every month! If this happens, the program should stop and display an appropriate error message.
If you accidentally create an infinite loop, try to interrupt
or
restart
the kernel.
Use the modulus operator to separate out the years and months.
After completing the activity above, answer the following questions:
Describe in your own words how the mortgage calculator works. What are the steps you are computing? What are you keeping track of in each iteration?
If you had a mortgage with a principal of $250,000
, an interest rate
of 4%
, and a monthly payment of $1000
, how long will it take for you to
pay it off? How much will you have paid in total?
To submit your notebook, follow the same directions for Notebook 01, except store this notebook in the notebook03 folder.