Don't understand why my code is skipping to the end

69
January 31, 2019, at 05:00 AM

It used to work, now it doesn't! I'm still learning. I am trying to make a compound interest calculator, just so I can start learning code by doing it. I'm not quite sure why the code is skipping to the end after asking for an interest rate... I've been trying to figure it out for a while now and I can't figure it out. I'm sure it's something small. I'm a beginner so if someone could help me, it'd mean a lot.

import math
class error: # All possible errors are located here.
    def input():
        print("\nError: Invalid input entered.\n")
    def accumulated():
        print("\nError: There has been an error calculating the accumulated amount.\n")
    def principal():
        print("\nError: There has been an error calculating the principal amount.\n")
    def rate():
        print("\nError: There has been an error calculating the interest rate.\n")
    def years():
        print("\nError: There has been an error calculating the number of years.\n")
class user_input:
    def r_of_c():  # What gets assigned here is based upon the user's input based on its corrosoponding function.
        roc = int(input("\nEnter the rate of compounding: "))
        if roc == (1):
            n = 365
        elif roc == (2):
            n = 52
        elif roc == (3):
            n = 12
        elif roc == (4):
            n = 4
        elif roc == (5):
            n = 2
        elif roc == (6):
            n = 1
        elif roc == (7):
            n = 2.7182
        elif roc > 7:
            error.input()
        elif roc < 1:
            error.input()
        else:
            error.input()
    def one():
        if var == (1):
            p = float(input("\nPlease enter the Principal amount: $"))
            r = float(input("\nPlease enter an Interest rate: %"))
            print("\n[1] Daily [2] Weekly [3] Monthly [4] Quarterly [5] Semi-Annually [6] Annually [7] Continuously")
            r_of_c()
            t = int(input("\nPlease enter the number of years compounded: "))
            try: accumulated_amount = float(p * (1 + ((r/100)/n)) ** (n * t))
            except:
                error.accumulated()
        else:
            pass
    def two():
        if var == (2):
            a = float(input("\nPlease enter the Accumulated amount: $"))
            r = float(input("\nPlease enter an Interest rate: %"))
            print("\n[1] Daily [2] Weekly [3] Monthly [4] Quarterly [5] Semi-Annually [6] Annually [7] Continuously")
            r_of_c()
            t = int(input("\nPlease enter the number of years compounded: "))
            try: principal_amount = float(a / (1 + ((r/100)/n)) ** (n * t))
            except: 
                error.principal()
        else:
            pass
    def three():
        if var == (3):
            a = float(input("\nPlease enter the Accumulated amount: $\n"))
            p = float(input("\nPlease enter the Principal amount: $"))
            print("\n[1] Daily [2] Weekly [3] Monthly [4] Quarterly [5] Semi-Annually [6] Annually [7] Continuously")
            r_of_c()
            t = int(input("\nPlease enter the number of years compounded: "))
            try: interest_rate = float(n(math.pow(a/p, 1/nt) - 1))
            except:
                error.rate()
        else:
            pass
    def four():
        if var == (4):
            a = float(input("\nPlease enter the Accumulated amount: $\n"))
            p = float(input("\nPlease enter the Principal amount: $"))
            r = float(input("\nPlease enter an Interest rate: %"))  
            print("\n[1] Daily [2] Weekly [3] Monthly [4] Quarterly [5] Semi-Annually [6] Annually [7] Continuously")
            r_of_c()
            t = int(input("\nPlease enter the number of years compounded: "))
            try: number_of_years = ((math.log10(a/p) / n(math.log10(1 + (r/100)/n))))
            except:
                error.years()
        else:
            pass
    def greater_than():
        if var > 4:
            error.input()
        else:
            pass
    def less_than():
        if var < 1:
            error.input()
        else: 
            pass
class answer: # This will be processed based upon the intial user input.
    def one():
        if var == (1):
            try: print("\nThe accumulated amount is $%s.\n" % round(accumulated_amount, 2))
            except:
                pass
    def two():
        if var == (2):
            try: print("\nThe principal amount is $%s.\n" % round(principal_amount, 2))
            except:
                pass
    def three():
        if var == (3): 
            try: print("\nThe interest rate is $%s.\n" % interest_rate)
            except:
                pass
    def four():
        if var == (4):
            try: print("\nThe number of years compounded is $%s.\n" % round(number_of_years, 2))
            except:
                pass    
print("[1] Accumulated Amount [2] Principal [3] Interest Rate [4] Years \n")
var = int(input("What do you want to solve for? "))
""" The following will process the users input based on which function it gets assigned to. """
try: user_input.one()
except: 
    pass
try: user_input.two()
except:
    pass
try: user_input.three()
except:
    pass
try: user_input.four()
except:
    pass
try: user_input.greater_than()
except: 
    pass
try: user_input.less_than()
except:
    pass
""" The following will output the answer based on the initial user input. """
try: answer.one()
except:
    pass
try: answer.two()
except:
    pass
try: answer.three()
except: 
    pass
try: anwer.four()
except:
    pass
input("Press Enter to exit. ")
Answer 1

The reason it appears to skip to the end is because mostof the methods of class user_input, such as one(), two(), three(), etc try to call its r_of_c() method which causes a

NameError: name 'r_of_c' is not defined

exception to occur because it wasn't done properly.

This makes it so that when the following lines near the end are executed:

try: user_input.one()
except:
    pass
try: user_input.two()
except:
    pass
try: user_input.three()
except:
    pass
try: user_input.four()
except:
    pass
try: user_input.greater_than()
except:
    pass
try: user_input.less_than()
except:
    pass
""" The following will output the answer based on the initial user input. """
try: answer.one()
except:
    pass
try: answer.two()
except:
    pass
try: answer.three()
except:
    pass
try: anwer.four()
except:
    pass
input("Press Enter to exit. ")

every call to user.input.xxx() inside a try/except encounters an exception being raised, but they are ignored and the following call is attempted, and so on and so forth, until the input("Press Enter to exit. ") at the very end is encountered.

I think the proper way to call it would be user_input.r_of_c() instead of a plain r_of_c(). Note that because there are a number of other issues that also would need to be fixed just make this change won't be enough to make it work—but it should stop skipping to the end.

READ ALSO
Refactor java code with two loops use stream [on hold]

Refactor java code with two loops use stream [on hold]

Can someone help me to refactor my code use stream? I have no idea how to do it with "if" and nested loops

37
Claims Jwt Parser

Claims Jwt Parser

i've a problem with claims, i want generete a token with users permissions for intercept all routes and block if not authorized

89
How to replace this String operations with Bitwise operations?

How to replace this String operations with Bitwise operations?

Considering I have this fields in a Base object:

35
WordPress: Pagination for custom array

WordPress: Pagination for custom array

I have this snippet for creating a list of users sorted by the highest post views

53