Reputation: 26
I am working on some python homework for a guide I am following, and it is basically wanting to me to make a function that sets up an investment for someone. It would take how much they start with, how much interest is applied, and for how many years they will be investing. Pretty simple...
My hang up is on functions, and the best way to deploy them. As you can see in my code, I have a function set up, but it's not working right, and I didn't even bother calling it.
Instead, I kind of gorilla-ed my way through, and have it outputting what I want it to output, but I KNOW there is an easier way to do what I do.
I attempted to come up with a for loop to do the math from one year of investing to another, but I couldn't figure it out.
Also, I know my indents on lines 11-15 are off, but that's because I have my for loop commented out. The code works just fine without it being commented out as well.
Any help or criticism is welcomed and appreciated!
#function invest defines initial amount invested, interest, and over how many years
def invest(amount, rate, time):
for amount in invest:
amount = 5
print amount
return
mylist = [2000, .025, 5];
#for growth in "mylist[0, 1, 2]":
year1 = mylist[0] + (mylist[0] * mylist[1])
year2 = year1 + (year1 * mylist[1])
year3 = year2 + (year2 * mylist[1])
year4 = year3 + (year3 * mylist[1])
year5 = year4 + (year4 * mylist[1])
#invest(mylist);
print "prinicpal amount:", mylist[0]
print "annual rate of return:", mylist[1]
print "I am going to invest for", mylist[2], "years."
print
print "In doing, this I will see the following returns over the next 5 years:"
print "In the first year, I will see", year1
print "In the second year, I will see", year2
print "In the third year, I will see", year3
print "In the foruth year, I will see", year4
print "In the the fifth year, I will see", year5
Upvotes: 0
Views: 553
Reputation: 26
After getting with a friend at work who is a Python guru, we decided on doing a few different things for this piece of code I was working on. A lot of this is syntax that he recommended, after hearing my suggestions and thoughts. And then a lot of it is him saying, why would you want to do that when you could just do this?
The new code(using the compound
function) takes the same arguments I was using, but then allows you to pass arguments as you're calling the script itself, instead of having to modify the script every time you want to change a variable.
So you are now able to call the script, then add: amount invested, interest rate, time invested, and additional income added every year on top of the investment. Example..
python `invest.py 2000 .025 5 1000`
Doing this now returns exactly what I was looking for. I can't take all of the credit for this one, but I did want to at least show an example of an answer that I had landed on.
The function invest
was what I had come up with thanks to David Robinson's advice, and the function compound
is what my friend suggested. Both are usable, just need to comment out the other one.
#!/usr/bin/python
#function invest defines initial amount invested, interest, and over how many years
import sys
def invest(amount, rate, time):
growth = (amount * rate)
# range generates a list, thus using memory
# xrange is a generator so it doesn't use all memory
for i in xrange(1, time + 1):
print 'Year {0}: {1}'.format(i, amount + (growth * i))
return growth
def compound(amount, rate, time, additional=0):
# range generates a list, thus using memory
# xrange is a generator so it doesn't use all memory
for i in xrange(1, time + 1):
amount += additional
amount = int(amount + (amount * rate))
print 'Year {0}: {1}'.format(i, amount)
if __name__ == '__main__':
args = sys.argv
time = 5
if len(args) > 1:
amount = int(args[1])
rate = float(args[2])
time = int(args[3])
additional = int(args[4])
compound(amount, rate, time=time, additional=additional)
#growth = invest(2000, .025, time=time)
Upvotes: 1