2
votes

I have an initial value that's increasing at a certain percent annually(5.5%). My program is required to print out the year when the initial value is 2, 3, and 4 times greater than the original value using while loops only. So far, I'm using three while loops that finds when the initial value has been 2, 3, and 4 times greater than the original value but as of now it works, but it looks really repetitive. This is what I have so far:

anum 123.45    #initial value
anum2 = initial * 2    #supposedly the numbers to indicate when the initial value has doubled, tripled, quadrupled etc.
anum3 = initial * 3
anum4 = initial * 4
initialYear = 2017
year = initialYear    #year counter


while anum <= anum2:     #make the loop stop before going further than two times greater than initial value
    growth = anum * 0.055
    anum = growth + anum
    year += 1
print("Overall points will be 2 times 2017 points in {:d} years ({:d}).".format\
  (year - 2017, year))

while anum <= anum3:
    growth = anum * 0.055
    anum = growth + anum
    year += 1
print("Overall points will be 3 times 2017 points in {:d} years ({:d}).".format\
  (year - 2017, year))

while anum <= anum4:
    growth = anum * 0.055
    anum = growth + anum
    year += 1
print("Overall points will be 4 times 2017 points in {:d} years ({:d}).".format\
  (year - 2017, year))

Is there a way where I could potentially have this process going using a nested while loop in order to reduce its overall repetitiveness?

Any help is greatly appreciated!

3
I don't think you need to nest loops. I think you just need to merge the three loops into 1. - RobertB

3 Answers

1
votes

There's a way to do this with nested loops.

I'm just a beginner in python, but this might work:

anum = 123.45
multiplier = 2


while multiplier < 5:
        year = initialYear
        for i in range(multiplier):
                growth = anum * 0.055
                anum = growth + anum
                year += 1
        print("multiplier =", multiplier)
        multiplier += 1

I hope this helps!

1
votes

Here is a solution that works for me personally:

anum = 123.45
initialNum = anum
initialYear = 2017
year = initialYear
times = 2

while times < 5:
    targetnum = initialNum * times
    while anum <= targetnum:
        growth = anum * 0.055
        anum = growth + anum
        year += 1
    print("Overall points will be {:d} times 2017 points in {:d} years ({:d}).".format(times, year - initialYear, year))
1
votes

Although you've solved your problem, I'm giving an answer that packages your code in a function to avoid the nested while loops. This makes it much easier to reuse your code for any range of inputs (for example, maybe you don't want to start in 2017, maybe you don't always want the same interest rate). Below shows an example of switching inputs with d and not hard-coding the printed message or all the values.

def calc_year(initial_value, start_year, interest, target_multiplier):
    number_of_years = 0
    final_value = initial_value * target_multiplier

    while initial_value <= final_value:
        number_of_years += 1
        initial_value = (1 + interest) * initial_value

    print("Overall points will be {} times {} points in {} years ({})".format(
            target_multiplier, start_year, number_of_years,
            start_year + number_of_years))

a = calc_year(123.45, 2017, 0.055, 2)
b = calc_year(123.45, 2017, 0.055, 3)
c = calc_year(123.45, 2017, 0.055, 4)

# Now changing all the input values is easy
d = calc_year(321.98, 2022, 0.005, 5)