473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

hi i have got a problem writing the code for this program

2
this is the program as below and now what i need to do is add the following to it but i am not able to get how to do it
Expand|Select|Wrap|Line Numbers
  1. # futval.py
  2. #    A program to compute the value of an investment
  3. #    carried 10 years into the future
  4.  
  5. def main():
  6.     print "This program calculates the future value"
  7.     print "of a 10 year investment."
  8.  
  9.     principal = input("Enter the initial principal: ")
  10.     apr = input("Enter the annualized interest rate: ")
  11.  
  12.     for i in range(10):
  13.         principal = principal * (1 + apr)
  14.  
  15.     print "The value in 10 years is:", principal
  16.  
  17. main()
the output i get is

This program calculates the future value
of a 10 year investment.
Enter the initial principal: 1000
Enter the annualized interest rate: .1
The value in 10 years is: 2593.7424601

but i need to get an output like this as below

This program calculates the future value
of a 10 year investment.
Enter the initial principal: 1000
Enter the annualised interest rate: .1
Year Value
----------------
0 1100.00
1 1210.00
2 1331.00
3 1464.10
4 1610.51
5 1771.56
6 1948.72
7 2143.59
8 2357.95
9 2593.74
The value in 10 years is: 2593.74

so i would be thankful if anybody could help me out as i am new to python
Aug 18 '09 #1
2 2925
bvdet
2,851 Expert Mod 2GB
This can be done with string formatting, Look here under "6.6.2. String Formatting Operations" for documentation. I will post again with an example.

-BV
Aug 18 '09 #2
bvdet
2,851 Expert Mod 2GB
Print the table header:
Expand|Select|Wrap|Line Numbers
  1. >>> print "%-5s%s" % ("Year", "Value")
  2. Year Value
You can use string methods also:
Expand|Select|Wrap|Line Numbers
  1. >>> print "%s%s" % ("Year".center(6), "Value".center(8))
  2.  Year  Value  
Print the separator:
Expand|Select|Wrap|Line Numbers
  1. >>> print '-'*14
  2. --------------
Inside the for loop, print the intermediate results. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> p = 10
  2. >>> for i in range(10):
  3. ...     print "%-5s%.2f" % (i, p)
  4. ...     p *= 2
  5. ...     
  6. 0    10.00
  7. 1    20.00
  8. 2    40.00
  9. 3    80.00
  10. 4    160.00
  11. 5    320.00
  12. 6    640.00
  13. 7    1280.00
  14. 8    2560.00
  15. 9    5120.00
  16. >>> 
Aug 18 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: 100 | last post by:
Has anybody read Steve Maguire's book "Writing solid code"? Do you think that the ideas in this book are not applicable in c# language? Does anybody find if(2 == i) istead of if(i == 2) as...
16
by: lovecreatesbeauty | last post by:
`Writing C code is very simple', one guy related to my work said. I'm not sure whether he is an expert or not. What he said about C programming like this can't convince me. I think there should be...
14
by: harbintyler | last post by:
I was wondering if anyone knows how to write a tic tac toe game in c++?
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
111
by: Enteng | last post by:
Hi I'm thinking about learning C as my first programming language. Would you recommend it? Also how do you suggest that I learn it?What books/tutorials should I read for someone like me? Thanks...
0
by: artursam | last post by:
STUDENT TABLE NAME DATA TYPE DESCRIPTION IDNO CHARACTER ID Code "Primary Key" COURSE CHARACTER Course DEPART CHARACTER Department LNAME CHARACTER Last Name FNAME CHARACTER First Name...
1
by: lenest | last post by:
I need help writing a program.... You are to write a python program to accomplish the following: a.. Play a dice game of Craps using a random number generator to simulate the roll of the...
3
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID,...
7
by: jacob navia | last post by:
There are some people here (let's call them "regulars" for short) that are always giving lessons to people about how easy is to write portable code, etc. They always point fingers at you telling...
3
by: Levi Campbell | last post by:
Hi, I'm trying to write a program for a friend of mine who uses windows but I use GNU/Linux. I know you can use mingw and link to the python dll, but is there a way to create a win32 service under...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.