Question

In: Computer Science

How do I make this code print the st variable rounded to 2 decimal places? Everything...

How do I make this code print the st variable rounded to 2 decimal places? Everything I have tried gives me an error.

print('Enter output filename')
name=input()
print('Enter principal amount')
p=float(input())
print('Enter term length (month)')
n=int(input())
print('Enter annual interest')
i=float(input())
j=i/12
m=p*j/(1-(1+j)**-n)
file = open(name, "w")
title = ["Month, ", "Total Accured interest, ", "Loan Balance \n"]
file.writelines(title)
first_row=["0",",","$","0.00",",","$",p," \n"]
s1=' '.join([str(elem) for elem in first_row])
file.writelines(s1)
t=0
for j in range(n):
    tai=(p*i/12)
    t=t+tai
    p=(p-m)+tai
    st=""
    ls=[j+1,",","$",t,",","$",p,"\n"]
    st=''.join([str(elem) for elem in ls])
    file.writelines(st)
file.close()

Solutions

Expert Solution

Code Screenshot :

Executable Code:

print('Enter output filename')
name=input()
print('Enter principal amount')
p=float(input())
print('Enter term length (month)')
n=int(input())
print('Enter annual interest')
i=float(input())
j=i/12
m=p*j/(1-(1+j)**-n)
file = open(name, "w")
title = ["Month, ", "Total Accured interest, ", "Loan Balance \n"]
file.writelines(title)
first_row=["0",",","$","0.00",",","$",p," \n"]
s1=' '.join([str(elem) for elem in first_row])
file.writelines(s1)
t=0
for j in range(n):
   tai=(p*i/12)
   t=round(t+tai,2)
   p=round((p-m)+tai,2)
   st=""
   ls=[j+1,",","$",t,",","$",p,"\n"]
   st=''.join([str(elem) for elem in ls])
   file.writelines(st)
file.close()

Sample Output :

file.txt

Please comment below if you have any queries.
Please do give a thumbs up if you liked the answer thanks :)


Related Solutions

rounded off to 2 decimal places 1. FInd the ordinary interest on P500,000 at 5.5% invested...
rounded off to 2 decimal places 1. FInd the ordinary interest on P500,000 at 5.5% invested from April 1, 2017 to December 24, 2017? 2. How long will it take 200,000 to amount half a million pesos if the simple interest rate is 8.25% per annum? 3. Find the simple interest on 20,000 borrowed on May 1 and will be paid after 6months at 6.25% interest using exact time. 4. How much must be paid on the due date of...
I need my code output values edited to be formatted to two decimal places (i believe...
I need my code output values edited to be formatted to two decimal places (i believe using setprecision), i will need you to edit my code toformat each menu option to 2 decimal places: provided is my code #include <iostream> #include <iomanip> using namespace std; int main() {    double radius;    double base;    double height;    double length;    double width;    double Area;    const double PI = 3.14159;    int choice;    // display menu   ...
AZX 4.125% 2037 bonds are currently quoting at 104.15. Rounded to two decimal places as a...
AZX 4.125% 2037 bonds are currently quoting at 104.15. Rounded to two decimal places as a percent, the yield to maturity on these bonds is __________%.
Answer the following break even problems. In addition to the answers (rounded to 0 decimal places),...
Answer the following break even problems. In addition to the answers (rounded to 0 decimal places), show the spreadsheets you created to solve them (like you do for homeworks in lab). Next to any cells with equations, put the equation in text. For example, if I was doing a spreadsheet to sum the number of fish my son and I caught, it would look like: Q1 (5 points) Stew’s Plastics produces a variety of CD cases. The best-selling product is...
How do I make a template functions for these 2 functions to prevent code duplication These...
How do I make a template functions for these 2 functions to prevent code duplication These are the 2 functions 1st: virtual Card lead_card(const std::string &trump) {              int numOfTrump = 0;         for (int i = 0; i < int(hand.size()); ++i) {             if (hand[i].is_trump(trump) == true) {                 numOfTrump += 1;             }         }              if (numOfTrump != int(hand.size())) {             Card c = highestCardNotIncludingTrump(hand, trump);             int indexOfHighest = 0;             for (int i...
Based on the data shown below, calculate the correlation coefficient (rounded to three decimal places) x...
Based on the data shown below, calculate the correlation coefficient (rounded to three decimal places) x y 2 1.6 3 2.67 4 3.84 5 3.71 6 0.48 7 1.75 8 3.42 9 0.89 10 1.96 11 3.13 12 0.1
23. NOTE: Answers using z-scores rounded to 3 (or more) decimal places will work for this...
23. NOTE: Answers using z-scores rounded to 3 (or more) decimal places will work for this problem. The population of weights for men attending a local health club is normally distributed with a mean of 175-lbs and a standard deviation of 26-lbs. An elevator in the health club is limited to 32 occupants, but it will be overloaded if the total weight is in excess of 5952-lbs. a. Assume that there are 32 men in the elevator. What is the...
NOTE: Answers using z-scores rounded to 3 (or more) decimal places will work for this problem....
NOTE: Answers using z-scores rounded to 3 (or more) decimal places will work for this problem. The population of weights for men attending a local health club is normally distributed with a mean of 179-lbs and a standard deviation of 29-lbs. An elevator in the health club is limited to 34 occupants, but it will be overloaded if the total weight is in excess of 6460-lbs. Assume that there are 34 men in the elevator. What is the average weight...
Enter answers rounded to 5 decimal places. 1. In pharmacologic research a variety of clinical chemistry...
Enter answers rounded to 5 decimal places. 1. In pharmacologic research a variety of clinical chemistry measurements are routinely monitored closely for evidence of side effects of the medication under study. Suppose typical blood glucose levels are normally distributed with mean 90 mg/dL and standard deviation 38 mg/dL. 1a. If the normal range is 65-120 mg/dL, that what percentage of values will fall into the normal range? 1b. In some studies only values that are at least 1.5 times as...
Each bound should be rounded to three decimal places. A random sample of ?=100 observations produced...
Each bound should be rounded to three decimal places. A random sample of ?=100 observations produced a mean of ?⎯⎯⎯=32 with a standard deviation of ?=4 (a) Find a 95% confidence interval for ?μ Lower-bound:   Upper-bound: (b) Find a 90% confidence interval for ?μ Lower-bound:   Upper-bound: (c) Find a 99% confidence interval for ?μ Lower-bound:   Upper-bound:
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT