The following program has two mistakes in it that cause it to print an incorrect answer.

# Computes how much interest is earned on a given amount of principal.
principal = float(raw_input('Enter principal: '))
interestRate = float(raw_input('Enter interest rate as a percent: '))
years = int(raw_input('Enter number of years: '))
totInterest = 0.0
curYear = 0
while curYear <= years:
annualInterest = principal * interestRate
totInterest = totInterest + annualInterest
principal = principal + annualInterest
curYear = curYear + 1
print 'Total interest in ' + str(years) + ' year(s): $' + str(totInterest)

How do I correctly write the program to give me $100 for it is the correct answer.

Thanks,
MS

Recommended Answers

All 4 Replies

Welcome to daniweb! Please use code tags when posting code on daniweb.

[code]

...code here...

[/code]

For your question -- that depends on what your inputs are. What is the principal, interest, years?

Principal is 1000
Interest rate as a percent is 10
Number of years is 1
Total interest in 1 year is $120000.0

P.S. Sorry for my inability to code it

I suggest this

# Computes how much interest is earned on a given amount of principal.
principal = float(raw_input('Enter principal: '))
interestRate = float(raw_input('Enter interest rate as a percent: ')) / 100
years = int(raw_input('Enter number of years: '))
totInterest = 0.0
for curYear in range(years):
    annualInterest = principal * interestRate
    totInterest = totInterest + annualInterest
    principal = principal + annualInterest
print 'Total interest in ' + str(years) + ' year(s): $' + str(totInterest)

Your 2 mistakes are 1) Your interest rate is 10.0 instead of 0.10 and 2) You compute over 2 years instead of 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.