1

In my code, I ask for the value of two integers twice and I need to add the values entered for each int respectively

year_of_interest = int(input('Please enter the year that you want to calculate the personal interest rate for : '))
expenditure_categories = int(input('Please enter the number of expenditure categories: '))

for i in range(expenditure_categories):
    print
    expenses_prev_year = int(input('Please enter expenses for previous year: '))
    expenses_year_interest = int(input('Please enter expenses for year of interest: '))

total_expenses_prev_year = expenses_prev_year+expenses_year_interest
total_expenses_year_interest = expenses_year_interest+expenses_year_interest

inflation_rate = ((total_expenses_year_interest-total_expenses_prev_year)/total_expenses_year_interest)*100


print("Personal inflation rate for", str(year_of_interest), "is", str(inflation_rate))`

The inputs I provided were:

  • year_of_intrest = 2022
  • expenditure_categories = 2
  • expenses_prev_year = 100 and 200
  • expenese_year_of_interest = 100 and 300

The calculated inflation rate I get is 16.67, I should be getting 25

1

1 Answer 1

2

Try this, I'm basically just adding the values in the loop:

total_expenses_prev_year = 0
total_expenses_year_interest = 0
year_of_interest = int(input('Please enter the year that you want to calculate the personal interest rate for : '))
expenditure_categories = int(input('Please enter the number of expenditure categories: '))

for i in range(expenditure_categories):
    print
    expenses_prev_year = int(input('Please enter expenses for previous year: '))
    expenses_year_interest = int(input('Please enter expenses for year of interest: '))
    total_expenses_prev_year += expenses_prev_year
    total_expenses_year_interest += expenses_year_interest

inflation_rate = ((total_expenses_year_interest-total_expenses_prev_year)/total_expenses_year_interest)*100


print("Personal inflation rate for", str(year_of_interest), "is", str(inflation_rate))

Result:

Please enter the year that you want to calculate the personal interest rate for : 2022
Please enter the number of expenditure categories: 2
Please enter expenses for previous year: 100
Please enter expenses for year of interest: 100
Please enter expenses for previous year: 200
Please enter expenses for year of interest: 300
Personal inflation rate for 2022 is 25.0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.