-3

I was wondering if anyone could help me figure out how to do percentage addition in Python? I want to add exponential percentage growth to a given number i.e. 3% addition to 150 = 154.5, and then 3% to the new value but in a continuous string an x number of times.

like so:

1.) 150
2.) 154.5
3.) 159.135
4.) 163.90905
5.) 168.8263215
6.) 173.891111145
7.) 179.10784447935
8.) 184.4810798137305
9.) 190.0155122081424
10.) 195.7159775743867

Would love to be able to do this for 200 times and print in one go.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
YLE yup
  • 1
  • 1
  • Please provide us with code you made so far, so we can help you , without code we cant help you. – Thyrus Nov 29 '21 at 10:48
  • Can you focus and clarify your question on the exact problem? Are you finding difficulties in calculating the new value? Are you having problems doing it in a loop? How would you do this if you had to do it with a pen and paper? From there the transition to Python is really straightforward with basic loops – Tomerikoo Nov 29 '21 at 10:50
  • 2
    I mean, do you know how to add 3% to a number, outside of Python? – Tomerikoo Nov 29 '21 at 10:52

2 Answers2

0

What I would do is:

Create a generator used to do the exponentional percentage:

def expo_generator(x):
    while True:
        yield x
        x = x + x*3/100

Create a take utility (it takes n elements of a generator or array). (This util can be found also in more-itertools package)

from itertools import islice
def take(n, iterable):
    return list(islice(iterable, n)

Then you can use:

result = take(20, expo_generator(150))

It will return an array of the first 20 value of the expo_generator function.

ohe
  • 3,461
  • 3
  • 26
  • 50
0

There are basically two ways to do it:

  1. working with x in a loop:
x = 150
for n in range(200):
    x *= 1.03  # multiplying by 103%, therefore "adding 3%"
    print(x)
  1. mathematical way:
x = 150
for n in range(200):
    print(x * (1.03**n))  # a**b means a^b, exponetial function
white
  • 601
  • 3
  • 8