0

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.

6
  • 1
    Why not just change new_total = to total =, and print new_total to print total? Commented May 12, 2014 at 19:09
  • Are you sure that your print is at the right level - looks like it should be outside the for loop Commented May 12, 2014 at 19:11
  • @JonClements: I'm pretty sure the OP wanted one print per loop iteration (see desired output) Commented May 12, 2014 at 19:12
  • @David fair point... obviously wasn't paying attention... Commented May 12, 2014 at 19:13
  • 1
    This might be a little more clear (and probably faster) as total *= 1.10 Commented May 12, 2014 at 19:26

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

It seems to only still give one amount here, and the math isn't adding up. For an initial total of 54.13, the next iteration should come out to 59.54, and so forth, but all I'm getting returned here is one value of 104.39. I would like to print the next 10 values in the loop. I'm thinking it maybe ran the loop 10 times and just printed the last value.
@Janden See my edit. For the code posted I get the correct output.
That was it, Alex, thanks much! I looked at my code again after switching to total and realized the reason it was only printing once was because I wasn't indenting the print on the last line, so it wasn't part of the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.