ddonche
ddonche

Reputation: 1065

repeating a function in python (2.7)

I created a simple royalty calculator, but what I would like to now do is make it so that it separately calculates x number of times like this:

x = x + (x * .10) 

I need it to repeat incrementally with the total going into and setting the new value of x for the next iteration. So say I want to print out the results of that 10 times, with each time adding the new totals into the equation, so for example, these results might come up if you started with an amount of 1.00:

1.10
1.21 (1.10 + an extra 10 %)
1.33 
1.46

and so on for however many times you need. Here is what I was trying and I'm stuck on this. I am also thinking of making the user input the initial amount.

for _ in range(10):
    new_total = total + (total * .10)
    print new_total 

How can I make this happen? This only prints the first iteration and not all 10. Keep in mind that the total variable gets determined by the other parts of the script that works before this piece of code.

Upvotes: 0

Views: 115

Answers (1)

anon582847382
anon582847382

Reputation: 20391

Just replace the uses of new_total with the original total:

for _ in range(10):
    total = total + (total * .10)
    print total

The reason this happens is because although in the first iteration you set the value of new_total to a processed value, you don't actually change total so it will give the same result every time.


Demo (where total starts at 54.13) with Python 2.7:

59.543
65.4973
72.04703
79.251733
87.1769063
95.89459693
105.484056623
116.032462285
127.635708514
140.399279365

Upvotes: 3

Related Questions