pi = 3.14159
pi = round(pi,2) # round pi to 2 decimal points, then reassign to pi
print(pi)3.14
Assignment 3 will cover Control Flow and I/O. The overall goal of this assignment is to ensure that you are comfortable with Control Flow structures like if/else statements and for/while loops as well as how to interact with files in Python. Follow the steps below to ensure that you receive a passing grade:
- Complete the following tasks within this notebook
- When finished, convert this notebook to an HTML/PDF file
- Place the following items into a zip folder:
- assignment.HTML or assignment.PDF
- assignment.ipynb
- Name this folder using the naming convention: FIRSTNAME_LASTNAME_ASSIGNMENT3.zip
- For example, If your name was Jane Doe, then the zipped folder should be called JANE_DOE_ASSIGNMENT3.zip
Let’s say a person invests money in a savings account that yields interest. Assuming that the person leaves all interest on deposit in the account, your goal is to create a function that calculates and displays the amount of money in the account at the end of each year for \(10\) years. Use the following formula for determining these amounts:
\[ a = p(1 + r)^n \]
where
\(p\) is the original amount invested (i.e., the principal)
\(r\) is the annual interest rate
\(n\) is the number of years
\(a\) is the amount on deposit at the end of the \(n^{\ th}\) year.
In your function, let \(r\), the annual interest rate, be a default argument set to \(5\%\)
Use the round() function to round the compound interest to the nearest cent.
Hint: You will have to use a for loop and the range() function. Think about what the arguments for the range() function should be. Should the first value in the loop be \(0\) or \(1\)?
The file DeclarationOfIndependence.txt contains a transcript of the entire United States Declaration of Independence from 1776. Your goal is to open the file then find and print the longest word that is contained within the document.
Hint: You’ll want to use the split() function from Week 2. Also be sure to replace all \n (newline) characters with the a space using the replace() function.
Without the use of any external python packages, do the following;
Open the file dracula.txt
Read the file line by line and break the novel text into its individual paragraphs.
Build a python list called “paragraphs” whose elements are strings for each individual paragraph.
Paragraph definition:
Word definition
Only consider the text of the novel, i.e. remove the lines before and after the novel starts and finishes from your analysis.
At the end print the following quantities
NOTE: If you get stuck and cannot figure out how to proceed, then email your TA. They will provide you with a partial solution to help you move forward.
OPTIONAL EXTRA CREDIT * Repeat the exercise, but for sentences instead of paragraphs