Question

Python

4. while loops/String Manipulation Review (50%) The consumer price index (CPI) indicates the average price of a fixed basket

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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) -

main 1 if # input max inc-time. : integer - the maximu ber of times. max_inc_time int(input(Enter maximum number of times:

Output:

Enter maximum number of times: 6 Consumer price will be 2 times 2014 prices in 29 years 2043) Consumer price will be 3 times

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.

Add a comment
Know the answer?
Add Answer to:
Python 4. while loops/String Manipulation Review (50%) The consumer price index (CPI) indicates the average price...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT