Required code in Python 3 is -
Code:
if __name__ == '__main__':
# input 'max_inc_time' : integer - the maximum number
of 'times'
max_inc_time = int(input('Enter maximum number of
times: '))
# 'inc_time' : integer - variable to keep track of
times the CPI have increased
# initialized with which shows that minimum increase
that is to be calculated
inc_time = 2
# 'cpi_val_2014' : float - to store the value of CPI
in June 2014
cpi_val_2014 = 238.25
# 'rate' : float - to store the rate of increase of
CPI in future
rate = 0.025
# 'yrs_count' : integer - to store te numbr of years
to attain certain value of CPI i.e. years that is required as the
output
# initialized with 0 which represents that its 2014
(for this code) and no year have elapsed
yrs_count = 0
# 'cur_cpi_lvl' : float - to store the CPI value after
'yrs_count' number from 2014
# initialized with 'cpi_val_2014' which shows that
current year is 2014 (for this code) and no year have elapsed
cur_cpi_val = cpi_val_2014
# traversing the loop till 'inc_time' is less than
or equal to 'max_inc_time'
# thus outer loop count the number of 'times'
while inc_time <= max_inc_time:
# loop run till the current value
of CPI is not 'inc_time' of June 2014 CPI value
while cur_cpi_val < inc_time *
cpi_val_2014:
# adding the
increase of 2.5% (using 'rate') in current CPI value
'cur_cpi_val'
cur_cpi_val =
cur_cpi_val + cur_cpi_val * rate
# incrementing
the years count by 1 showing that one year have passed and the CPI
value have increased.
yrs_count +=
1
# printing the required
output
print('Consumer price will be %d
times 2014 prices in %d years ( %d )' %(inc_time, yrs_count,
yrs_count + 2014) )
# incrementing the number of time
CPI have increased
inc_time += 1
ScreenShot of code in editor (for better visualization of indentation) -
Output:
Note:
The Python version used for the execution of code is 3.6.1. The outer loop runs one less than the maximum number of times or 'max_inc_time'. While the inner loop runs till the current value of CPI is not equal to required times (more precisely, 'inc_time') of June 2014 CPI value. 'cur_cpi_val' is increased at a rate of 2.5% per annum which is reflected in line 24.
Python 4. while loops/String Manipulation Review (50%) The consumer price index (CPI) indicates the average price...