I have the following which should return a value of the interest where it should be divided by 100. How do I implement this?
import math
p = int(raw_input("Please enter deposit amount: \n"))
r = float(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 (float(A))
else:
print(float(B))
Try something like this (Hint you don't need to import math for something like this):
from decimal import *
p = Decimal(raw_input("Please enter deposit amount:"))
r = Decimal(raw_input("Please input interest rate as a percentage:")) /100
t = int(raw_input("Please insert number of years of the investment:"))
n = 1 # You should really be asking how many times is the interest compounded per year? If the user chooses compound...
A = p*(1 + r)**t
B = p*(1 + r)**(n*t)
while(True):
interest = raw_input("Do you want simple or compound interest?")
if(interest.lower() == "simple"):
print(A)
break
elif(interest.lower() == "compound"):
print(B)
break
Try it here!
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
so im getting a list index out of range here:
for i in range(len(empNr)):
print(empNr[i], rate[i], hrs[i], wkPay[i])
and I haven't really figured lists out, so I may just be confused and am unable to understand why i would be out of range. here the rest of the code below. thanks!
SENTINEL = 0
wkTotal = payHigh = wkPay = empHigh = 0
empNr = []
rate = []
hrs = []
wkPay = []
i = 0
empNr.append(input("Input first employee number: "))
#iteration if 0 is entered it should stop
while (empNr[i] != str(SENTINEL)):
rate.append(int(input("Input rate: ")))
hrs.append(int(input("Input hours: ")))
wkPay.append(rate[i] * hrs[i])
i = i + 1
empNr.append(input("Input employee number or '0' to stop: "))
#calculations using list functions
wkTotal = sum(wkPay)
payHigh = max(wkPay)
wkAvg = float(wkTotal) / len(empNr)
#output summary for pay calculator
print("\n\n Data entry complete " +" \n --------------------------------------")
print("\n Employee Number Pay Rate Hours worked Pay ")
print("--------------------------------------------------------")
for i in range(len(empNr)):
print(empNr[i], rate[i], hrs[i], wkPay[i])
print("Summary: ")
print("WEEK TOTAL: ", str(wkTotal))
print("EMPLOYEE HIGH: ", str(empHigh))
print("PAY HIGH: ", str(payHigh))
print("WEEK AVERAGE: ", str(wkAvg))
empNr.append(input("Input employee number or '0' to stop: ")) this line appends '0' to empNr. But it has no corresponding values for rate, hrs or wkPay. Meaning rate, hrs or wkPay these have one element less than that of empNr. A quick fix (but not recommended) would be to loop for rate or hrs instead of empNr. So your code would be:
for i in range(len(rate)):
print(empNr[i], rate[i], hrs[i], wkPay[i])
A better fix would be:
i = 0
inp = input("Do you want to add anew record? Y/n: ")
while (inp == "Y"):
empNr.append(input("Input first employee number: "))
rate.append(int(input("Input rate: ")))
hrs.append(int(input("Input hours: ")))
wkPay.append(rate[i] * hrs[i])
i = i + 1
inp = input("Do you want to add anew record? Y/n: ")
So while I enter 'Y' I can add new entries and length of each empNr, rate, hrs, wkPay would match. If I enter anything other than Y, the loop will terminate and the lengths will still remain the same...
SENTINEL = 0
wkTotal = payHigh = wkPay = empHigh = 0
empNr = []
rate = []
hrs = []
wkPay = []
i = 0
empNr.append(input("Input first employee number: "))
#iteration if 0 is entered it should stop
while (empNr[i] != str(SENTINEL)):
rate.append(int(input("Input rate: ")))
hrs.append(int(input("Input hours: ")))
wkPay.append(rate[i] * hrs[i])
i = i + 1
empNr.append(input("Input employee number or '0' to stop: "))
#calculations using list functions
wkTotal = sum(wkPay)
payHigh = max(wkPay)
wkAvg = float(wkTotal) / len(empNr)
#output summary for pay calculator
print("\n\n Data entry complete " +" \n --------------------------------------")
print("\n Employee Number Pay Rate Hours worked Pay ")
print("--------------------------------------------------------")
for i in range(len(empNr)-1):
print(empNr[i], rate[i], hrs[i], wkPay[i])
print("Summary: ")
print("WEEK TOTAL: ", str(wkTotal))
print("EMPLOYEE HIGH: ", str(empHigh))
print("PAY HIGH: ", str(payHigh))
print("WEEK AVERAGE: ", str(wkAvg))
Please use the above.
Lets say the list had 2 employee details. According to your solution len(empNr) gives 3. The range function will make the for loop iterate for 0,1 and 2. Also one mistake you did was even for the exiting condition when u r accepting the value for 0 you had used the same list which increases the count of the list by 1 to the no. of employees. Hence do a -1 so that it iterates only for the employee count
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 get the error "TypeError: can't multiply sequence by non-int of type 'str'" in line 5 which is the "number = str(c(c*r)**x)" this is a weak attempt at making a growth calculator. Any help would be appriciated I am fairly new
import math
c = raw_input("what is the intial number?")
r = raw_input("What is the rate of growth?")
x = raw_input("How many years are taking place?")
int(c)
int(r)
int(x)
number = str(c(c*r)**x)
print (number)
You forgot to reassign the variables:
import math
c = raw_input("what is the intial number?")
r = raw_input("What is the rate of growth?")
x = raw_input("How many years are taking place?")
c = int(c)
r = int(r)
x = int(x)
number = str(c*(c*r)**x)
print (number)
The better way to do in the same statement only, like this (user friendly way).
import math
c = int(raw_input("what is the intial number?\n=> "))
r = int(raw_input("What is the rate of growth?\n=> "))
x = int(raw_input("How many years are taking place?\n=> "))
number = str(c*(c*r)**x)
print(number)
Output
what is the intial number?
=> 3
What is the rate of growth?
=> 4
How many years are taking place?
=> 5
746496
int() returns the parsed integer. You also need to put * for each multiplication operation you're doing.
print can print integers by default, no need for the str around number
import math
c = raw_input("what is the intial number?")
r = raw_input("What is the rate of growth?")
x = raw_input("How many years are taking place?")
c = int(c)
r = int(r)
x = int(x)
number = (c * (c * r)) ** x
print(number)
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