I'm trying to develop a program that will formulate interest rate according to user input over x amount of years.
interestRateYear = int(input("Please enter the year that you want to calculate the personal interest rate for : "))
expenditureCategories = int(input("Please enter the number of expenditure categories: "))
According to the user input for expenditureCategories, the following questions will be asked x amount of times.
previousYearExpenses= int(input("Please enter expenses for previous year: "))
expensesOfInterest= int(input("Please enter expenses for year of interest: "))
I tried creating a loop statement
while expenditureCategories>=0:
previousYearExpenses= int(input("Please enter expenses for previous year: "))
expensesOfInterest= int(input("Please enter expenses for year of interest: "))
previousYearExpenses = previousYearExpenses + 1
expensesOfInterest = expensesOfInterest + 1
I'm new to coding and I'm not sure what I'm doing wrong.
This should do the work:
interestRateYear = int(input("Please enter the year that you want to calculate the personal interest rate for : "))
expenditureCategories = int(input("Please enter the number of expenditure categories: "))
for i in range(expenditureCategories): #This will repeat expenditureCategories number of times
previousYearExpenses= int(input("Please enter expenses for previous year: "))
expensesOfInterest= int(input("Please enter expenses for year of interest: "))
previousYearExpenses = previousYearExpenses + 1
expensesOfInterest = expensesOfInterest + 1
You forgot to substract numbers from the expeditureCategories counter. Now, if on the while loop, you want to ask the question the amount of times that is in the counter, it should be while > 0, because if you leave it as >=0 it will do it one more time than the amount of times you want. Now, i know youre starting and probably wont care for now, but if you want to keep learning, just know that if you know the amount of loops youre going to do, a for loop is more efficient than a while loop.
If you want to keep your original code,
while expenditureCategories > 0:
previousYearExpenses= int(input("Please enter expenses for previous year: "))
expensesOfInterest= int(input("Please enter expenses for year of interest: "))
previousYearExpenses = previousYearExpenses + 1
expensesOfInterest = expensesOfInterest + 1
expenditureCategories = expenditureCategories - 1
This should work.
Related
I am working on my first project where I ask users if they are want to calculate an investment or bond. Within this I've tried to nest another if/elif/else statement after they input investment or bond to get the user to input the necessary information for either for me to make the calculation but Python is registering an error for when I want to ask users about the bond calculation. I've tried to make it as clear as possible below:
import math
#Menu Option for Users
print("Investment: Calculate the amount of interest you'll earn on your investment")
print("Bond: Calculate the amount you'll have to pay on your home loan")
type = input("Please input either Investment or Bond to proceed:")
#casefold removes case sensitivity so user can input with or without caps.
#Investment Calculation
if type.casefold() == "Investment":
#if user inputs investment, they need to input the following information
money = input("Please input your deposit amount: ")
ir = input("Please input your interest rate as a percentage: ")
time = input("Please input how long you plan to invest for in years: ")
interest = input("Please input if you would like simple or compound interest: ")
#initialisations
simple_ir_calc = 0
comp_ir_calc = 0
if interest.casefold() == "simple":
simple_ir_calc = money (1 + ((ir/100) * time))
print(f"You will make {simple_ir_calc} on your investment")
elif interest.casefold() == "compound":
comp_ir_calc = money * math.pow((1+ (ir/100)), time)
print(f"You will make {comp_ir_calc} on your investment")
else:
print("Error. Either enter simple or compound.")
#Bond calculation
elif type.casefold() == "Bond":
#user inputs info needed to calc bond
pv = input("Please enter the present value of your property: ")
ir_b = input("Please input the interest rate as a percentage: ")
time_m = input("Please enter the number of months needed to repay the bond: ")
repayment = 0
repayment = ((ir_b/12) * pv) / math.pow(1 - (1 +(ir_b/12)), (-time))
print(f"You will have to pay {repayment} each month.")
else:
print("Error. Either input investment or bond.")
I tried to fix the indentations - so now the only 2 problems that python highlights is the expressions on line 34 and 44.
Also as a sidenote the bond repayment formula is meant to be X = (ir.pv)/(1-(1+ir)^(-time))
I just included -time at the end but I have no idea if my formula works within python
I know this is probably riddled with errors, so if you see anything else please let me know! I am very new to both python and stackoverflow so sorry for so many questions within one :(
I think this sorts out the indentation but only you know your intentions. A general comment - the code all fails if the user does not enter precisely one of your expected answers. You should pick up input errors. This might be easiest using 'Enter 1 for A, 2 for B etc'.
import math
#initialisations
simple_ir_calc = 0
comp_ir_calc = 0
#Menu Option for Users
print("Investment: Calculate the amount of interest you'll earn on your investment")
print("Bond: Calculate the amount you'll have to pay on your home loan")
choose = input("Please input either Investment or Bond to proceed:")
#casefold removes case sensitivity so user can input with or without caps.
print(choose.casefold())
#Investment Calculation
if choose.casefold() == "investment":
#if user inputs investment, they need to input the following information
money = input("Please input your deposit amount: ")
ir = input("Please input your interest rate as a percentage: ")
time = input("Please input how long you plan to invest for in years: ")
interest = input("Please input if you would like simple or compound interest: ")
if interest.casefold() == "simple":
simple_ir_calc = money (1 + ((ir/100) * time))
print(f"You will make {simple_ir_calc} on your investment")
elif interest.casefold() == "compound":
comp_ir_calc = money * math.pow((1+ (ir/100)), time)
print(f"You will make {comp_ir_calc} on your investment")
else:
print("Error. Either enter simple or compound.")
#Bond calculation
elif choose.casefold() == "bond":
#user inputs info needed to calc bond
pv = input("Please enter the present value of your property: ")
ir_b = input("Please input the interest rate as a percentage: ")
time_m = input("Please enter the number of months needed to repay the bond: ")
repayment = ((ir_b/12) * pv) / math.pow(1 - (1 +(ir_b/12)), (-time_m))
print(f"You will have to pay {repayment} each month.")
else:
print("Error. Either input investment or bond.")
Looks like indents are totally messed.
It should look like that:
# Condition
if a == 0:
b = 1
elif a == 1:
b = 2
else:
# Nested conditions
if c:
b = 3
else:
b = 9
I have a question where im asked to calculate interest rate of an account after asking user for:
P is the present value of the account.
i is the monthly interest rate.
t is the number of months.
The current code I have is this:
def get_value(p, i , t):
return p * (1 + i) ** t
def main():
p = float(input("Please enter the current amount of money in your account: "))
i = float(input("Please enter the monthly interest rate: "))
t = float(input("Please enter the number of months: "))
#the while loop would probably go here, but I just dont know how to do it#
future_total = get_value(p, i, t)
print ("\nAfter", t, "months, you will have $", format(future_total, ".2f"), "in your account.")
main()
But the output is only giving me the final amount after 10 months, how do I implement a loop in order to see how much money would be in the account since month 1?
I would first make a variable called month and set it equal to 1. Then I would use the while loop so that when the month is less than the inputted month, the present value will be updated based on the inputted information. This will print out the money in the account for each month and not just the final value.
def get_value(p, i , t):
month = 1
while month <= t:
p = p * (1 + i)
print(month, p)
month += 1
def main():
p = float(input("Please enter the current amount of money in your account: "))
i = float(input("Please enter the monthly interest rate: "))
t = float(input("Please enter the number of months: "))
print(get_value(p,i,t))
# future_total = get_value(p, i, t)
# print ("\nAfter", t, "months, you will have $", format(future_total, ".2f"), "in your account.")
# print(f'After {t} months, you will have ${future_total} in your account.')
main()
i am trying to get the percentage increase over a period of selected months .i cant seem to get a hold on how to make use of the current month value in calculating for the new month and summing all togther .
NOC = int(input('Please enter the number of chickens : '))
MONTHS = int(input('Please enter the number of months for the estimate : '))
FCE = NOC*10*MONTHS/100
if NOC == '' and MONTHS =='' :
print('please Enter a value for month and year')
else:
print('Your Result',FCE)
If you are looking for compound increase then you need to iterate over each month and use the last months value to increase
NOC = int(input('Please enter the number of chickens : '))
MONTHS = int(input('Please enter the number of months for the estimate : '))
for month in range(0, MONTHS):
NOC = NOC + (NOC*0.1)
print(NOC)
I am trying to recreate a problem from our classwork: Write a program that can handle a shopping event. First, it requests the number of items bought, then asks for each items' price and tax rate. Then prints the total cost.
Example:
How many items you bought: 2
For item 1
Enter the price: 10
Enter the tax rate: 0
For item 2
Enter the price: 20
Enter the tax rate: 8
Your total price is 31.6
I have no knowledge on how I would compute for items larger than 1. ie my code works for 1 item.
items = int(input("How many items did you buy? "))
for i in range(1, items+1, 1):
print("For item ",i)
price = float(input("Enter the price: "))
tax_rate = float(input("Enter the tax rate: "))
total = price + price*(tax_rate/100)
print("Your total price is", total)
I need to somehow save the totals after each iteration and add them all up. I am stumped.
Note: This is an introduction to python course and also my first programming course. We've only learned for loops thus far.
You need to have an initialized counter to have a running total.
items = int(input("How many items did you buy? "))
total = 0
for i in range(1, items+1, 1):
print("For item ",i)
price = float(input("Enter the price: "))
tax_rate = float(input("Enter the tax rate: "))
total += price + price*(tax_rate/100)
print("Your total price is", total)
It is almost perfect just the calculation is giving the wrong result, this is because its dividing the answer by 100
I have to divide the interest rate by 100. So the r value entered by the user needs to be divided by 100
import math
p = int(raw_input("Please enter deposit amount: \n"))
r = int(raw_input("Please input interest rate: \n")/100)
t = int(raw_input("Please insert number of years of the investment: \n"))
interest = raw_input("Do you want a simple or compound interest ? \n")
A = p*(1+r**t)
B = p*(1+r)^t
if interest == "simple":
print (A)
else:
print(B)
Like ettanany said, the main problem was the line on the interest rate input. Also you had a ^ instead of ** on your line 9. I finally added a bit on the if statement to make sure that you only print the result from the compound interest if the user entered the word "compound". Otherwise anything would lead to that unwanted choice.
import math
p = int(input("Please enter deposit amount: \n"))
r = int(input("Please input interest rate: \n"))/100
t = int(input("Please insert number of years of the investment: \n"))
interest = input("Do you want a simple or compound interest ? \n")
A = p*(1+r**t)
B = p*(1+r)**t
if interest == "simple":
print (A)
elif interest == "compound":
print(B)
else:
print("Error on the interest type")
So, you just need to change this line:
r = int(raw_input("Please input interest rate: \n")/100)
To:
r = int(raw_input("Please input interest rate: \n"))/100
raw_input("Please input interest rate: \n")/100 means you're dividing a string by 100 the you cast the result to int. Instead, you need to cast then the user input to int and you divide it by 100.
Note: In Python 2, 10/100 for example returns 0, so you many need to divide by 100.0 instead of 100.
>>> 10/100
0
>>> 10/100
0
>>> 10/100.0
0.1