compound_interest

Two versions of the code. One is using 'for' and the other is using 'while'
 avatar
unknown
python
a year ago
1.1 kB
6
Indexable
def saving_plan_calculator_using_while(deposit, interest, years):
    years_data = [deposit, 0]
    counter = 0
    while counter < years:
        current_year = (years_data[0] + years_data[1]) * interest
        # store the current year number inside years_data list
        years_data[1] = current_year
        print(f'End of year {counter + 1}: {round(current_year, 1)}')
        counter += 1
    print(f'Total deposits: {counter * deposit}')


def saving_plan_calculator_using_for(deposit, interest, years):
    years_data = [deposit, 0]
    for year in range(1, years + 1):
        current_year = (years_data[0] + years_data[1]) * interest
        # store the current year number inside years_data list
        years_data[1] = current_year
        print(f'End of year {year}: {round(current_year, 1)}')
    print(f'Total deposits: {years * deposit}')


print('Using while loop:')
saving_plan_calculator_using_while(10000, 1.05, 3)
print('-----------------------------------------')
print('Using for loop:')
saving_plan_calculator_using_for(10000, 1.05, 3)
Editor is loading...
Leave a Comment