473,805 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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 2949
bvdet
2,851 Recognized Expert Moderator Specialist
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 Recognized Expert Moderator Specialist
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
2939
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 unnetural and does it lead to more bugs in the code because of it makes programms hard to read. And my last question is: "Do you think that using boolean expressions
16
1680
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 two kinds of people can make such a comment on C programming. One is C expert with rich experiences of some years on real projects; The other is opportunist idiocy of C programming. What do you think of C programming? Is writing C code an art?...
14
14418
by: harbintyler | last post by:
I was wondering if anyone knows how to write a tic tac toe game in c++?
19
2452
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; //every class may have this statement self.hello = function() {
111
5606
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 in advance! -entengk
0
1007
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 MNAME CHARACTER Middle Name STREET CHARACTER Street CITY CHARACTER City
1
6229
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 dice, the code for the rolling of the dice should take place in a user written module named rolldice. b.. The rules of the game are as follows:
3
2699
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, lsRecordType, lsXNbr, lsFiscYr, "Beg", CStr(H.BegBalAccDepn), CStr(H.BegBalCost), CStr(H.BegBalCostReval), CStr(H.BegBalDepCost), CStr(H.BegBalDepnReval)) The program is running from within a Virtual PC
7
1880
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 you how bad you write code. One of them (I would say the prototype of them) is Heathfield. Here is an exchange that happened just a few hours ago, that demonstrates my point:
3
1651
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 Linux?
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.