| | |
Control Structures in Python
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
The following program has two mistakes in it that cause it to print an incorrect answer.
How do I correctly write the program to give me $100 for it is the correct answer.
Thanks,
MS
python Syntax (Toggle Plain Text)
# 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
Last edited by John A; Aug 21st, 2009 at 12:28 am. Reason: added code tags
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?
[code]
...code here...
[/code]
For your question -- that depends on what your inputs are. What is the principal, interest, years?
I suggest this
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.
python Syntax (Toggle Plain Text)
# 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)
Last edited by Gribouillis; Aug 21st, 2009 at 3:48 am.
![]() |
Similar Threads
- Starting Python (Python)
- Python good for anything standalone? (Python)
- goto control structure in the works for php 5.3 (PHP)
- python and bash (Python)
- Custom tree Control (Python)
- Python's Snack Module (Python)
- Python to Java: Questions I Have (Java)
- Tools for making a web based User interface in python (Python)
- Sorting in Python (Python)
Other Threads in the Python Forum
- Previous Thread: slow mystring = str(myint)
- Next Thread: file handling in python
| Thread Tools | Search this Thread |
abrupt ansi anti apache approximation array assignment backend beginner binary book builtin calculator character chmod converter countpasswordentry curved customdialog dan08 dictionaries dictionary drive dynamic examples exe file filename float format function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pysimplewizard python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame write wxpython






