Question

Programming language: PYTHON Prompt: You will be creating a program to show final investment amounts from...

Programming language: PYTHON
Prompt: You will be creating a program to show final investment amounts from a principal using simple or compound interest. First, get a principal amount from the user. Next, ask the user if simple or compound interest should be used. If the user types in anything other than these options, ask again until a correct option is chosen. Ask the user to type in the interest rate as a percentage from 0 to 100%. Finally, ask the user for the number of years of this investment. If simple interest is chosen: For each year, show the user the year number and total amount of interest earned to date. At the end, show the user the principal, the length of the investment, the type of interest and at what percentage, and the total final amount (i.e., principal plus total interest). If compound interest is chosen: For each year, show the user the year number and new principal. At the end, show the user the original principal, the length of the investment, the type of interest and at what percentage, and the final principal. The program only needs to run once per execution.
Key concepts and resources from class :

• While

• External resources and prior knowledge

Assumptions, checks, or procedures:

1. The user should be able to type “s” (lowercase, no quotes) for simple interest and “c” (lower case, no quotes) for compound interest. Anything other than “s” or “c” should be considered an invalid choice. Please see samples below.

2. The interest rate as a percentage (when the user types it in) must be a number between 0.0 and 100.0. For example, an interest rate of 4.5% would be entered as 4.5.

3. You will not be using any econ or finance formulas to calculate simple or compound interest. Instead, you will be doing the calculations “manually”.

4. You can assume that time is finite (the number of years entered by the user), the interest rate does not change over time, and interest is calculated once per year. You can consider any total amounts you show to be “end of year” amounts.

5. Decimal values are allowed. Round decimals in outputs to the nearest hundredth (i.e., same as you would see at a bank).

6. You will need to add dollar and percent signs to your output.

7. You can assume that inputs for principal, interest rate, and year will be valid. Inputs for year will not contain decimals. How to break down this prompt I’m going to heavily paraphrase your econ and finance classes (and we can make use of some assumptions) in describing the types of interest. Simple interest is calculated only on the principal and then summed yearly per the prompt. Compound interest is calculated on the new principal each year. Once you understand those differences, it’s just a matter of using while and other concepts to do that in code.

This is the code i have so far...this is the error i am getting : "Traceback (most recent call last): File "main.py", line 18, in while (i <= years): Type error: '<' not supported between instances of 'str'

principal = input("enter the principal amount:\t")

s = input("1. s for simple interest\n 2. c for compound interest\n enter your choice:\t")

while(s!='s' and s!='c'):

s = raw_input("1. s for simple interest\n2. c for compound interest\nenter your choice:\t")

if(s=='s'):

years = input("enter number of years:\t")

rateofinterest = input("enter rate of interest:\t")

i = 1

while(i<=years):

interest = (principal*i*rateofinterest)/100

amount = interest + principal

print("at the end of {} year interest:\t{} interest type: simple interest amount:\t{}".format(i,interest,amount))

i = i + 1

if(s=='c'):

years = input("enter number of years:\t")

rateofinterest = input("enter rate of interest:\t")

i = 1 while(i <= years):

interest = (principal*i*rateofinterest)/100

amount = interest + principal

print("at the end of {} year interest:\t{} interest type: compound interest amount:\t{}".format(i,interest,amount))

i = i + 1

principal = principal + interest

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

The main error in your code was the way you were trying to read values. The input() method returns a string value, not a numeric type. So principal=input(….) will read the principal value as a string and not floating point. If you need to read as float value, you should use float(input()) instead of input(), the same applies for int(). There were many other errors too. Fixed everything

#code

# the input() returns a string, not a numeric type
#if you need to input a floating point value, you should use float(input())
#if you need to input a integer value, you should use int(input())

#getting principal as a float value

principal = float(input("enter the principal amount:\t"))

#getting choice as string
s = input("1. s for simple interest\n2. c for compound interest\nenter your choice:\t")

#loops until a valid choice is made
while(s!='s' and s!='c'):
    s = input("1. s for simple interest\n2. c for compound interest\nenter your choice:\t")

if(s=='s'):
    #getting years as int and rate of interest as float
   
years = int(input("enter number of years:\t"))
    rateofinterest = float(input("enter rate of interest:\t"))
    i = 1
    while(i<=years):
        interest = (principal*i*rateofinterest)/100
        amount = interest + principal
        #displaying year and interest earned. here {:.2f} will round the
        #decimal values into 2 decimal places and then display
       
print("at the end of {} year interest earned:\t${:.2f}".format(i,interest))
        i = i + 1
    #displaying principal, length, type , interest rate and final amount
   
print('principal: ${:.2f}, length of investment: {} years, type: simple interest'.format(principal,years),end=' ')
    print('interest rate: {:.2f}%, total final amount: ${:.2f}'.format(rateofinterest,amount))
if(s=='c'):
    years = float(input("enter number of years:\t"))
    rateofinterest = float(input("enter rate of interest:\t"))
    #finding starting principal
   
current_principal=principal
    #finding the rate in which each time the interest is compounded in a year
   
compound_interest_rate=(rateofinterest/12)/100
    i = 1
    while(i <= years):
        n=1
        #loops for 12 times (months in a year)
       
while n<=12:
            #finding interest
           
interest = current_principal * compound_interest_rate
            #updating principal
            
current_principal = current_principal + interest
            n+=1
        #displaying principal at the end of year
       
print("at the end of {} year principal amount:\t${:.2f}".format(i,current_principal))
        i = i + 1
    #displaying original principal, duration, interest rate and final principal
   
print('original principal: ${:.2f}, length of investment: {} years, type: compound interest'.format(principal, years), end=' ')
    print('interest rate: {:.2f}%, total principal amount: ${:.2f}'.format(rateofinterest, current_principal))

#output

enter the principal amount:         125000

1. s for simple interest

2. c for compound interest

enter your choice:            c

enter number of years:   5

enter rate of interest:     7.25

at the end of 1 year principal amount:     $134369.79

at the end of 2 year principal amount:     $144441.92

at the end of 3 year principal amount:     $155269.04

at the end of 4 year principal amount:     $166907.74

at the end of 5 year principal amount:     $179418.86

original principal: $125000.00, length of investment: 5.0 years, type: compound interest interest rate: 7.25%, total principal amount: $179418.86

Add a comment
Know the answer?
Add Answer to:
Programming language: PYTHON Prompt: You will be creating a program to show final investment amounts from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in Python that computes the interest accrued on an account. You can modify...

    Write a program in Python that computes the interest accrued on an account. You can modify the “futval.py” program given in Chapter 2 to accomplish it. Your program should use a counted loop rather than a formula. It should prompt the user to enter the principal amount, the yearly interest rate as a decimal number, the number of compounding periods in a year, and the duration. It should display the principal value at the end of the duration. To compute...

  • This C++ Program should be written in visual studio 2017 You are to write a program...

    This C++ Program should be written in visual studio 2017 You are to write a program that can do two things: it will help users see how long it will take to achieve a certain investment goal given an annual investment amount and an annual rate of return; also, it will help them see how long it will take to pay off a loan given a principal amount, an annual payment amount and an annual interest rate. When the user...

  • solution: new file, invest.cpp 2. Prompt and read in the initial investment (type integer). 3. Prompt...

    solution: new file, invest.cpp 2. Prompt and read in the initial investment (type integer). 3. Prompt and read in the interest rate (type integer). 4. Prompt and read in the number of years (type double). 5. Compute the final value of the investment as: x*(1 + (R/100))' where x is the initial investment, R is the interest rate, and Y is the number of years. Use the C++ function pow to compute (1 + (R/100))'. Note that you will compute...

  • 14.Compound Interest hank account pays compound interest, it pays interest not only on the principal amount...

    14.Compound Interest hank account pays compound interest, it pays interest not only on the principal amount that was deposited into the account, but also on the interest that has accumulated over time. Suppose you want to deposit some money into a savings account, and let the account earn compound interest for a certain number of years. The formula for calculating the balance of the account afer a specified namber of years is The terms in the formula are A is...

  • using this code to complete. it python 3.#Display Program Purpose print("The program will show how much...

    using this code to complete. it python 3.#Display Program Purpose print("The program will show how much money you will make working a job that gives a inputted percentage increase each year") #Declaration and Initialization of Variables userName = "" #Variable to hold user's name currentYear = 0 #Variable to hold current year input birthYear = 0 #Variable to hold birth year input startingSalary = 0 #Variable to hold starting salary input retirementAge = 0 #Variable to hold retirement age input...

  • Write a program that prints the accumulated value of an initial investment invested at a specified...

    Write a program that prints the accumulated value of an initial investment invested at a specified annual interest and compounded annually for a specified number of years. Annual compounding means that the entire annual interest is added at the end of a year to the invested amount. The new accumulated amount then earns interest, and so forth. If the accumulated amount at the start of a year is acc_amount, then at the end of one year the accumulated amount is:...

  • C Programming For this task, you will have to write a program that will prompt the...

    C Programming For this task, you will have to write a program that will prompt the user for a number N. Then, you will have to prompt the user for N numbers, and print then print the sum of the numbers. For this task, you have to create and use a function with the following signature: int sum(int* arr, int n); Your code should look something like this (you can start with this as a template: int sum(int* arr, int...

  • in python language. 3. Calculates the final amount (A). The program uses four inputs: P, n,r,...

    in python language. 3. Calculates the final amount (A). The program uses four inputs: P, n,r, and t. Hint: nt 1 = P(1+3) Where • P = principal amount (initial investment) . r= annual nominal interest rate (as a decimal) • n = number of times the interest is compounded per year . t = number of years

  • It's my python assignment. Thank you You wrote a program to prompt the user for hours...

    It's my python assignment. Thank you You wrote a program to prompt the user for hours and rate per hour to compute gross pay. Also your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours. Enter Hours: 45 Enter Rate: 10 Pay: 475.0 (475 = 40 * 10 + 5 * 15) Rewrite your pay program using try and except so that your program handles non-numeric input gracefully. Enter Hours: 20 Enter...

  • Creating a Calculator Program

    Design a calculator program that will add, subtract, multiply, or divide two numbers input by a user.Your program should contain the following:-The main menu of your program is to continue to prompt the user for an arithmetic choice until the user enters a sentinel value to quit the calculatorprogram.-When the user chooses an arithmetic operation (i.e. addition) the operation is to continue to be performed (i.e. prompting the user for each number, displayingthe result, prompting the user to add two...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT