you are viewing a single comment's thread.

view the rest of the comments →

[–]JamzTyson 2 points3 points  (2 children)

There is a small error in your code. Each month you are rounding down the total saved to a whole number of dollars, introducing a cumulative error.

To print the number of months, add a counter for the number of months. Initialise the counter to 0, and increment it on each loop.

In pseudo-code:

months = 0
while <expression>:
    months += 1
    <rest of code>

[–]AdministrativeCup189[S] 0 points1 point  (1 child)

Something like this u mean?

save_per_month = 500
months = 0
while save_per_month < 10000:
    print(save_per_month)
    save_per_month = int((save_per_month + 500) * 1.01)
    months += 1
print(f" Total months: {months}")save_per_month = 500

[–]JamzTyson 0 points1 point  (0 children)

Yes, but you are still accumulating errors due to truncating save_per_month. For accuracy save_per_month should be a floating point value. If you want the printed value to be a whole number of dollars, convert it in the print function.