Having trouble with python ,by dividing the answer - python

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

Related

Nesting an if condition within an if condition gone wrong? +(Investment and Bond Repayment Calculations in Python)

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

PYTHON: Using users answers to repeat a question x amount of times

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.

Write a Python program to print the following sequence of numbers (N to be entered by user): X + X^3/3! + X^5/5!... Upto the Nth Term

The problem i am having with this question is that the code i have written is showing a logical error. Here is the code i have written:
x = int(input("Please enter the base number: "))
n = int(input("Please enter the number of terms: "))
s = 0
factorial = 1
for i in range(1,n+1,2):
factorial = factorial*i
s = (x**i) / factorial
#using f string to format the numbers to a fixed number of decimal places
print(f"{s:.6f}", end='+')
I used the for loop to only show odd values of index, because that is the requirement in the question. My output is coming as follows:
Please enter the base number: 2
Please enter the number of terms: 4
2.000000+2.666667+
We don't have to actually find the sum, only display all the individual addends separated by a plus sign. What changes should i make within the code to get the required results?
My required output would look something like this:
Please enter the base number: 2
Please enter the number of terms: 4
2.000000+1.333333+0.266666+0.025396
Just change to
for i in range(1,2*(n)+1,2):
from
for i in range(1,n+1,2):
Now, the output is:
Please enter the base number: 2
Please enter the number of terms: 4
2.000000+2.666667+2.133333+1.219048+
Also, the method of calculating factorial is wrong as it skips half of the terms, when you jump i from 1 to 3 to 5, so that 2, 4, 6.. are getting missed.
So, you can do:
if i > 1:
factorial = factorial*i*(i-1)
elif i == 1:
factorial*i
So, the final code would be:
x = int(input("Please enter the base number: "))
n = int(input("Please enter the number of terms: "))
s = 0
factorial = 1
for i in range(1,2*n+1,2):
if i > 1:
factorial = factorial*i*(i-1)
elif i == 1:
factorial*i
s = (x**i) / factorial
#using f string to format the numbers to a fixed number of decimal places
print(f"{s:.6f}", end='+')
Factorial is in the loop but the loop does i=1,3,5 and not i=1,2,3,4,5, that's might be a problem. If the "number of terms" : "2.000000+2.666667" is two so in the loop your range avec to be until n*2 and not n but careful because the factory will be changed.

How to introduce a while loop in order to calculate monthly interest rate?

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 need to convert the interest rate to a decimal value

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!

Categories

Resources