Python function

I want to get output from this code but there is something wrong.an error message display when running the programe

#!/usr/bin/env python 3
def calculate_future_value(monthly_investment,yearly_interest,years):
    mothly_interest_rate=yearly_interest/12/100
    months=years*12
    

    future_value=0.0
    for i in range(0, months):
        future_value+=monthly_investment
        monthly_interest=future_value * monthly_interest_rate
        future_value+=monthly_interest
    return future_value

def main():
    choice="y"
    while choice.lower()=="y":
        monthly_investment=float(input("enter monthly investment:\t"))
        yearly_interest=float(input("enter yearly interst rate:\t"))
        years=int(input("enter number of years:\t"))
        future_value=calculate_future_value(monthly_investment,yearly_interest,years)
        print("future value:\t\t\t" +str(round(future_value, 2)))
        print()
        choice=input("continue? (y/n):")
        print()
    print("bye")    
if __name__ == "__main__":
    main()

errors are nice, they generally tell you what is wrong. Code that fails without an error, that is terrifying.

anyway, the error tells you that you have an undefined variable here:

monthly_interest=future_value * monthly_interest_rate

so then lets see where this variable is defined:

mothly_interest_rate=yearly_interest/12/100

the variable should be defined and within the scope, so the next thing to check is spelling.

i spotted a typo

1 Like