Control Structures in Python

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 3
Reputation: smuteekaanya is an unknown quantity at this point 
Solved Threads: 0
smuteekaanya smuteekaanya is offline Offline
Newbie Poster

Control Structures in Python

 
0
  #1
Aug 14th, 2009
The following program has two mistakes in it that cause it to print an incorrect answer.
  1. # Computes how much interest is earned on a given amount of principal.
  2. principal = float(raw_input('Enter principal: '))
  3. interestRate = float(raw_input('Enter interest rate as a percent: '))
  4. years = int(raw_input('Enter number of years: '))
  5. totInterest = 0.0
  6. curYear = 0
  7. while curYear <= years:
  8. annualInterest = principal * interestRate
  9. totInterest = totInterest + annualInterest
  10. principal = principal + annualInterest
  11. curYear = curYear + 1
  12. 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
Last edited by John A; Aug 21st, 2009 at 12:28 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,263
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 582
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Control Structures in Python

 
0
  #2
Aug 20th, 2009
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?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: smuteekaanya is an unknown quantity at this point 
Solved Threads: 0
smuteekaanya smuteekaanya is offline Offline
Newbie Poster

Re: Control Structures in Python

 
0
  #3
Aug 20th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 947
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 217
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: Control Structures in Python

 
0
  #4
Aug 21st, 2009
I suggest this
  1. # Computes how much interest is earned on a given amount of principal.
  2. principal = float(raw_input('Enter principal: '))
  3. interestRate = float(raw_input('Enter interest rate as a percent: ')) / 100
  4. years = int(raw_input('Enter number of years: '))
  5. totInterest = 0.0
  6. for curYear in range(years):
  7. annualInterest = principal * interestRate
  8. totInterest = totInterest + annualInterest
  9. principal = principal + annualInterest
  10. 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.
Last edited by Gribouillis; Aug 21st, 2009 at 3:48 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: smuteekaanya is an unknown quantity at this point 
Solved Threads: 0
smuteekaanya smuteekaanya is offline Offline
Newbie Poster

Re: Control Structures in Python

 
0
  #5
Aug 21st, 2009
Thanks alot.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC