Search code examples
pythonpython-3.xargs

Internal check system error - passing command line arguments


Hi I was working on Credit Calculator project at HyperSkill, while checking the result of my code for the last stage of the Credit Calculator, I am getting "Internal System Check Error" when I try to get it checked. The code is working on my IDE(pycharm), but somehow getting "Internal System Check Error". Could you please help to figure it out?

Project description: In this stage, you need to implement the following features:

Calculating differentiated payment. To do this, the user may run the program specifying the interest, the number of periods, and the credit principal. The ability to calculate the same values as in the previous stage for annuity payment (principal, count of periods, and the value of payment). The user specifies all the known parameters using command-line arguments, so there will be one unknown parameter. This is the value that the user wants to calculate. Handling invalid parameters. It's a good idea to show an error message Incorrect parameters if the parameters are invalid. The final version of your program is supposed to work from the command line and parse the following parameters:

  • --type, which indicates the type of payment: "annuity" or "diff" (differentiated). If --type is specified neither as "annuity" nor as "diff", or not specified at all, show the error message.

python creditcalc.py --principal=1000000 --periods=60 --interest=10 Incorrect parameters

  • --payment, which refers to the monthly payment. For --type=diff, thepayment is different each month, so we cannot calculate a number of periods or the principal, therefore, its combination with --payment is invalid, too:

python creditcalc.py --type=diff --principal=1000000 --interest=10 --payment=100000 Incorrect parameters

  • --principal is used for calculating both types of payment. You can get its value knowing the interest, the annuity payment, and the number of periods.
  • --periods parameter denotes the number of months and/or years needed to repay the credit. It's calculated based on the interest, annuity payment, and the principal.
  • --interest is specified without the percentage sign. Note that it may accept a floating-point value. Our credit calculator can't calculate the interest, so these parameters are incorrect:

python creditcalc.py --type=annuity --principal=100000 --payment=10400 --periods=8 Incorrect parameters

My code:

# Stage 4/4 of the credit calculator project
import sys
import math

args = sys.argv

if len(sys.argv) == 5:
    type1 = sys.argv[1].split('=')[1]
    credit_principal = int(sys.argv[2].split('=')[1])
    periods = int(sys.argv[3].split('=')[1])
    credit_interest = float(sys.argv[4].split('=')[1])
    nominal_interest = credit_interest / (12 * 100)

    if type1 == 'diff' or type1 == 'annuity' and credit_principal > 0 \
            and credit_interest > 0 and periods > 0:

        if type1 == "diff":

            sum_diff_payments = 0
            for i in range(1, periods + 1):
                montly_diff_payment = math.ceil(credit_principal / periods + nominal_interest * (
                        credit_principal - credit_principal * (i - 1) / periods))
                sum_diff_payments += montly_diff_payment
                print(f"Month {i}: payment is {montly_diff_payment}")
            print("\nOverpayment =", int(sum_diff_payments - credit_principal))

        else:
            if sys.argv[2].split('=')[0] == '--principal' and sys.argv[3].split('=')[0] == '--periods':

                annuity = math.ceil(credit_principal * (nominal_interest * math.pow(1 + nominal_interest, periods) / (
                        math.pow(1 + nominal_interest, periods) - 1)))
                print(f"Your annuity payment =  {annuity}!")
                print("Overpayment =", int(annuity * periods - credit_principal))

            elif sys.argv[2].split('=')[0] == '--payment' and sys.argv[3].split('=')[0] == '--periods':

                annuity = int(sys.argv[2].split('=')[1])
                credit = math.floor(annuity / ((nominal_interest * math.pow(1 + nominal_interest, periods)) / (
                            math.pow(1 + nominal_interest, periods) - 1)))
                print(f"Your credit principal = {credit}!")
                print("Overpayment = ", annuity * periods - credit)
                #Your credit principal = 800018!
                #Overpayment = 246622

            else:

                payment = int(sys.argv[3].split('=')[1])

                months = math.ceil(math.log(payment / (payment - nominal_interest * credit_principal),
                                            1 + nominal_interest))
                years = months // 12
                months = months % 12
                if months != 0:
                    print(f"You need {years} years and {months} months to repay this credit!")
                else:
                    print(f"You need {years} ", "years" if years > 1 else "year", "to repay this credit!")

                print("Overpayment =", int(payment * (years * 12 + months) - credit_principal))

    else:
        print("Incorrect parameters")
        sys.exit()

else:
    print("Incorrect parameters")
    sys.exit()

Solution

  • My plausible explanation is that its happening due to an endless loop, most likely an error waiting to parse the system argument vectors. There is also the same problem mentioned here(Credit Calculator Stage 4 - Internal System Check Error) with no solutions proposed.

    Instead of parsing the input through the cmd-line try and use a module such as argparse and pre-set the values of the input.

    Here is an out-of-context example showing how it's used

    import argparse
        
    # check command line options
    parser = argparse.ArgumentParser()
    
    parser.add_argument("--type", dest="type_of_payment", type=str, choices=["diff", "annuity"], required=True, help="Add some help text here")
    parser.add_argument("--periods", dest="period_count", type=int, required=True)
    parser.add_argument("--payment", dest="monthly_payment", type=float, required=True)
    parser.add_argument("--interest", dest="credit_interest", type=float, required=True)
    parser.add_argument("--principal", dest="credit_principal", type=float, required=True)
    
    args = parser.parse_args()
    
    # show the values
    print(f"Type of payment: {args.type_of_payment}")
    print(f"Periods: {args.period_count}")
    print(f"Payment: {args.monthly_payment}")
    print(f"Interest: {args.credit_interest}")
    print(f"Principal: {args.credit_principal}")