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

Question:

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 🙁

Asked By: Iffah

||

Answers:

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
Answered By: Ivan Perehiniak

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.")
Answered By: user19077881