Gurshaan
Gurshaan

Reputation: 27

How to add multiple values of an integer

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:

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

Upvotes: 1

Views: 144

Answers (1)

Cow
Cow

Reputation: 3040

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

Upvotes: 2

Related Questions