Skip to main content
Tweeted twitter.com/StackCodeReview/status/1241152660000198656
Spelling and grammar
Source Link
Toby Speight
  • 81.8k
  • 14
  • 101
  • 309

simplifying and compacting a A sequence/series formula solver

so iI made a formula retriever that works and allows importing of fractions because this.

This python code is meant for the fx-cg50 calculator's micropython, hencewhere there are alota lot of functions that dontdon't work including fractions, some mathematical functions in math like gcd or isclose, so isuch as math.gcd and math.isclose. So I really require some advice or coding tricks to simplify my program, Disclaimer: im only an a level student of 16 years, consider me a beginner, and i know eval is unsecured but im not planning on uploading this online, its only for my personal use.

Disclaimer: I'm only an A-Level student of 16 years; consider me a beginner. I know eval is insecure, but I'm not planning on uploading this online; it's only for my personal use.

# just to make the input_checker function smaller and to eliminate repeated code
def three_variables_looper_arithmetic():
    a = input("enter a1: ")
    b = input("enter n: ")
    c = input("enter d: ")
    original = 0
    count_list = [[a], [b], [c]]
    loop = 0
    for count in count_list:
        if isinstance(count[0], str):
            original = float(eval(count[0]))
            count_list[loop][0] = original
        loop += 1
    return count_list[0][0], count_list[1][0], count_list[2][0]

# just to make the input_checker function smaller and to eliminate repeated code
def three_variables_looper_geometric():
    a = input("enter a1: ")
    b = input("enter r: ")
    c = input("enter n: ")
    original = 0
    count_list = [[a], [b], [c]]
    loop = 0
    for count in count_list:
        if isinstance(count[0], str):
            original = float(eval(count[0]))
            count_list[loop][0] = original
        loop += 1
    return count_list[0][0], count_list[1][0], count_list[2][0]


# loops through all the inputs of a given situation based on whether its arithmetic
# or not, and checks whether the input is string like "6/2" so it could evaluate it, allows input of fractions
def input_checker(arithmetic_1_geometric_2, formula_num, L):
    if arithmetic_1_geometric_2 == 1:
        if formula_num == 1:
            return three_variables_looper_arithmetic()


        elif formula_num == 2:
            return three_variables_looper_arithmetic()

        elif formula_num == 3:
            a1 = input("enter a1: ")
            b = input("enter n: ")
            c = input("enter d: ")
            original = 0
            count_list = [[a1], [b], [c], [L]]
            loop = 0
            for count in count_list:
                if isinstance(count[0], str):
                    original = float(eval(count[0]))
                    count_list[loop][0] = original
            return count_list[0][0], count_list[1][0], count_list[2][0], count_list[3][0]
            
    elif arithmetic_1_geometric_2 == 2:
        if formula_num == 1:
            return three_variables_looper_geometric()
        
        elif formula_num == 2:
            return three_variables_looper_geometric()

        elif formula_num == 3:
            a1 = input("enter a1: ")
            b = input("enter n: ")
            original = 0
            count_list = [[a1], [b]]
            loop = 0
            for count in count_list:
                if isinstance(count[0], str):
                    original = float(eval(count[0]))
                    count_list[loop][0] = original
            return count_list[0][0], count_list[1][0]

# as this code is for a calculator the a and b buttons are right beside each other, so after you find your desired result
# you enter a to stop and b to continue 
def stopper():
    stop_flag = False
    stop_or_continue = ""
    while stop_or_continue != "a" or "b":
        stop_or_continue = input("Stop?: ")
        if stop_or_continue == "a":
            stop_flag = True
            break
        if stop_or_continue == "b":
            stop_flag = False
            break
    if stop_flag:
        raise SystemExit


print("Sequence & Series Solver")

# asks whether you want to solve arithmetically or geometrically, depends on the sequence/series
while True:
    choice = ""
    choices = ["a", "b", "c", "d"]
    while choice not in choices:
        choice = input("a for arithmetic\nb for geometric\n>> ")

    if choice == "a":
        arithmetic_1_geometric_2 = 1
        choice_2 = input("a for a_nth term\nb for sum\n>> ")

        # the variable arithmetic_1_geometric_2 refers to whether the inputs are for arithmetic hence it is 1, or the inputs
        # are for geometric hence 2
        # formula_num refers to the types of formulas you'll use in sequences/series
        if choice_2 == "a":
            print("a_nth=a1+(n-1)d")
            formula_num = 1
            a1, n, d = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = a1+(n-1)*d
            print(result)

        elif choice_2 == "b":
            print("Sn=(n/2)(2a1+(n-1)d)\nSn=(n/2)(a1+L)\nEnter x if L is unknown")
            L = input("Enter L: ")
            if L == "x":
                formula_num = 2
                a1, n, d = input_checker(arithmetic_1_geometric_2, formula_num, L)
                result = (n/2)*(2*a1+(n-1)*d)
                print(result)
            else:
                formula_num = 3
                a1, n, d, L = input_checker(arithmetic_1_geometric_2, formula_num, L)
                result = (n/2)*(a1+L)
                print(result)
    
    elif choice == "b":
        arithmetic_1_geometric_2 = 2
        choice_2 = input("a for a_nth term\nb for sum\nc for sum_to_inf")

        if choice_2 == "a":
            print("a_nth=a1(r)^(n-1)")
            formula_num = 1
            a1, r, n = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = a1*(r)**(n-1)
            print(result)

        elif choice_2 == "b":
            print("Sn=(a1(1-(r)^n))/(1-r)")
            formula_num = 2
            a1, r, n = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = (a1(1-(r)**n))/(1-r)
            print(result)

        elif choice_2 == "c":
            print("S_inf=a1/(1/r)")
            formula_num = 3
            a1, r = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = a1/(1-r)
            print(result)
    
    stopper()
```

simplifying and compacting a sequence/series formula solver

so i made a formula retriever that works and allows importing of fractions because this python code is meant for the fx-cg50 calculator's micropython, hence there are alot of functions that dont work including fractions, some functions in math like gcd or isclose, so i really require some advice or coding tricks to simplify my program, Disclaimer: im only an a level student of 16 years, consider me a beginner, and i know eval is unsecured but im not planning on uploading this online, its only for my personal use.

# just to make the input_checker function smaller and to eliminate repeated code
def three_variables_looper_arithmetic():
    a = input("enter a1: ")
    b = input("enter n: ")
    c = input("enter d: ")
    original = 0
    count_list = [[a], [b], [c]]
    loop = 0
    for count in count_list:
        if isinstance(count[0], str):
            original = float(eval(count[0]))
            count_list[loop][0] = original
        loop += 1
    return count_list[0][0], count_list[1][0], count_list[2][0]

# just to make the input_checker function smaller and to eliminate repeated code
def three_variables_looper_geometric():
    a = input("enter a1: ")
    b = input("enter r: ")
    c = input("enter n: ")
    original = 0
    count_list = [[a], [b], [c]]
    loop = 0
    for count in count_list:
        if isinstance(count[0], str):
            original = float(eval(count[0]))
            count_list[loop][0] = original
        loop += 1
    return count_list[0][0], count_list[1][0], count_list[2][0]


# loops through all the inputs of a given situation based on whether its arithmetic
# or not, and checks whether the input is string like "6/2" so it could evaluate it, allows input of fractions
def input_checker(arithmetic_1_geometric_2, formula_num, L):
    if arithmetic_1_geometric_2 == 1:
        if formula_num == 1:
            return three_variables_looper_arithmetic()


        elif formula_num == 2:
            return three_variables_looper_arithmetic()

        elif formula_num == 3:
            a1 = input("enter a1: ")
            b = input("enter n: ")
            c = input("enter d: ")
            original = 0
            count_list = [[a1], [b], [c], [L]]
            loop = 0
            for count in count_list:
                if isinstance(count[0], str):
                    original = float(eval(count[0]))
                    count_list[loop][0] = original
            return count_list[0][0], count_list[1][0], count_list[2][0], count_list[3][0]
            
    elif arithmetic_1_geometric_2 == 2:
        if formula_num == 1:
            return three_variables_looper_geometric()
        
        elif formula_num == 2:
            return three_variables_looper_geometric()

        elif formula_num == 3:
            a1 = input("enter a1: ")
            b = input("enter n: ")
            original = 0
            count_list = [[a1], [b]]
            loop = 0
            for count in count_list:
                if isinstance(count[0], str):
                    original = float(eval(count[0]))
                    count_list[loop][0] = original
            return count_list[0][0], count_list[1][0]

# as this code is for a calculator the a and b buttons are right beside each other, so after you find your desired result
# you enter a to stop and b to continue 
def stopper():
    stop_flag = False
    stop_or_continue = ""
    while stop_or_continue != "a" or "b":
        stop_or_continue = input("Stop?: ")
        if stop_or_continue == "a":
            stop_flag = True
            break
        if stop_or_continue == "b":
            stop_flag = False
            break
    if stop_flag:
        raise SystemExit


print("Sequence & Series Solver")

# asks whether you want to solve arithmetically or geometrically, depends on the sequence/series
while True:
    choice = ""
    choices = ["a", "b", "c", "d"]
    while choice not in choices:
        choice = input("a for arithmetic\nb for geometric\n>> ")

    if choice == "a":
        arithmetic_1_geometric_2 = 1
        choice_2 = input("a for a_nth term\nb for sum\n>> ")

        # the variable arithmetic_1_geometric_2 refers to whether the inputs are for arithmetic hence it is 1, or the inputs
        # are for geometric hence 2
        # formula_num refers to the types of formulas you'll use in sequences/series
        if choice_2 == "a":
            print("a_nth=a1+(n-1)d")
            formula_num = 1
            a1, n, d = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = a1+(n-1)*d
            print(result)

        elif choice_2 == "b":
            print("Sn=(n/2)(2a1+(n-1)d)\nSn=(n/2)(a1+L)\nEnter x if L is unknown")
            L = input("Enter L: ")
            if L == "x":
                formula_num = 2
                a1, n, d = input_checker(arithmetic_1_geometric_2, formula_num, L)
                result = (n/2)*(2*a1+(n-1)*d)
                print(result)
            else:
                formula_num = 3
                a1, n, d, L = input_checker(arithmetic_1_geometric_2, formula_num, L)
                result = (n/2)*(a1+L)
                print(result)
    
    elif choice == "b":
        arithmetic_1_geometric_2 = 2
        choice_2 = input("a for a_nth term\nb for sum\nc for sum_to_inf")

        if choice_2 == "a":
            print("a_nth=a1(r)^(n-1)")
            formula_num = 1
            a1, r, n = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = a1*(r)**(n-1)
            print(result)

        elif choice_2 == "b":
            print("Sn=(a1(1-(r)^n))/(1-r)")
            formula_num = 2
            a1, r, n = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = (a1(1-(r)**n))/(1-r)
            print(result)

        elif choice_2 == "c":
            print("S_inf=a1/(1/r)")
            formula_num = 3
            a1, r = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = a1/(1-r)
            print(result)
    
    stopper()
```

A sequence/series formula solver

I made a formula retriever that works and allows importing of fractions.

This python code is meant for the fx-cg50 calculator's micropython, where there are a lot of functions that don't work including fractions, some mathematical functions such as math.gcd and math.isclose. So I really require some advice or coding tricks to simplify my program.

Disclaimer: I'm only an A-Level student of 16 years; consider me a beginner. I know eval is insecure, but I'm not planning on uploading this online; it's only for my personal use.

# just to make the input_checker function smaller and to eliminate repeated code
def three_variables_looper_arithmetic():
    a = input("enter a1: ")
    b = input("enter n: ")
    c = input("enter d: ")
    original = 0
    count_list = [[a], [b], [c]]
    loop = 0
    for count in count_list:
        if isinstance(count[0], str):
            original = float(eval(count[0]))
            count_list[loop][0] = original
        loop += 1
    return count_list[0][0], count_list[1][0], count_list[2][0]

# just to make the input_checker function smaller and to eliminate repeated code
def three_variables_looper_geometric():
    a = input("enter a1: ")
    b = input("enter r: ")
    c = input("enter n: ")
    original = 0
    count_list = [[a], [b], [c]]
    loop = 0
    for count in count_list:
        if isinstance(count[0], str):
            original = float(eval(count[0]))
            count_list[loop][0] = original
        loop += 1
    return count_list[0][0], count_list[1][0], count_list[2][0]


# loops through all the inputs of a given situation based on whether its arithmetic
# or not, and checks whether the input is string like "6/2" so it could evaluate it, allows input of fractions
def input_checker(arithmetic_1_geometric_2, formula_num, L):
    if arithmetic_1_geometric_2 == 1:
        if formula_num == 1:
            return three_variables_looper_arithmetic()


        elif formula_num == 2:
            return three_variables_looper_arithmetic()

        elif formula_num == 3:
            a1 = input("enter a1: ")
            b = input("enter n: ")
            c = input("enter d: ")
            original = 0
            count_list = [[a1], [b], [c], [L]]
            loop = 0
            for count in count_list:
                if isinstance(count[0], str):
                    original = float(eval(count[0]))
                    count_list[loop][0] = original
            return count_list[0][0], count_list[1][0], count_list[2][0], count_list[3][0]
            
    elif arithmetic_1_geometric_2 == 2:
        if formula_num == 1:
            return three_variables_looper_geometric()
        
        elif formula_num == 2:
            return three_variables_looper_geometric()

        elif formula_num == 3:
            a1 = input("enter a1: ")
            b = input("enter n: ")
            original = 0
            count_list = [[a1], [b]]
            loop = 0
            for count in count_list:
                if isinstance(count[0], str):
                    original = float(eval(count[0]))
                    count_list[loop][0] = original
            return count_list[0][0], count_list[1][0]

# as this code is for a calculator the a and b buttons are right beside each other, so after you find your desired result
# you enter a to stop and b to continue 
def stopper():
    stop_flag = False
    stop_or_continue = ""
    while stop_or_continue != "a" or "b":
        stop_or_continue = input("Stop?: ")
        if stop_or_continue == "a":
            stop_flag = True
            break
        if stop_or_continue == "b":
            stop_flag = False
            break
    if stop_flag:
        raise SystemExit


print("Sequence & Series Solver")

# asks whether you want to solve arithmetically or geometrically, depends on the sequence/series
while True:
    choice = ""
    choices = ["a", "b", "c", "d"]
    while choice not in choices:
        choice = input("a for arithmetic\nb for geometric\n>> ")

    if choice == "a":
        arithmetic_1_geometric_2 = 1
        choice_2 = input("a for a_nth term\nb for sum\n>> ")

        # the variable arithmetic_1_geometric_2 refers to whether the inputs are for arithmetic hence it is 1, or the inputs
        # are for geometric hence 2
        # formula_num refers to the types of formulas you'll use in sequences/series
        if choice_2 == "a":
            print("a_nth=a1+(n-1)d")
            formula_num = 1
            a1, n, d = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = a1+(n-1)*d
            print(result)

        elif choice_2 == "b":
            print("Sn=(n/2)(2a1+(n-1)d)\nSn=(n/2)(a1+L)\nEnter x if L is unknown")
            L = input("Enter L: ")
            if L == "x":
                formula_num = 2
                a1, n, d = input_checker(arithmetic_1_geometric_2, formula_num, L)
                result = (n/2)*(2*a1+(n-1)*d)
                print(result)
            else:
                formula_num = 3
                a1, n, d, L = input_checker(arithmetic_1_geometric_2, formula_num, L)
                result = (n/2)*(a1+L)
                print(result)
    
    elif choice == "b":
        arithmetic_1_geometric_2 = 2
        choice_2 = input("a for a_nth term\nb for sum\nc for sum_to_inf")

        if choice_2 == "a":
            print("a_nth=a1(r)^(n-1)")
            formula_num = 1
            a1, r, n = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = a1*(r)**(n-1)
            print(result)

        elif choice_2 == "b":
            print("Sn=(a1(1-(r)^n))/(1-r)")
            formula_num = 2
            a1, r, n = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = (a1(1-(r)**n))/(1-r)
            print(result)

        elif choice_2 == "c":
            print("S_inf=a1/(1/r)")
            formula_num = 3
            a1, r = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = a1/(1-r)
            print(result)
    
    stopper()
Source Link
Anonymous
  • 1.2k
  • 1
  • 9
  • 21

simplifying and compacting a sequence/series formula solver

so i made a formula retriever that works and allows importing of fractions because this python code is meant for the fx-cg50 calculator's micropython, hence there are alot of functions that dont work including fractions, some functions in math like gcd or isclose, so i really require some advice or coding tricks to simplify my program, Disclaimer: im only an a level student of 16 years, consider me a beginner, and i know eval is unsecured but im not planning on uploading this online, its only for my personal use.

# just to make the input_checker function smaller and to eliminate repeated code
def three_variables_looper_arithmetic():
    a = input("enter a1: ")
    b = input("enter n: ")
    c = input("enter d: ")
    original = 0
    count_list = [[a], [b], [c]]
    loop = 0
    for count in count_list:
        if isinstance(count[0], str):
            original = float(eval(count[0]))
            count_list[loop][0] = original
        loop += 1
    return count_list[0][0], count_list[1][0], count_list[2][0]

# just to make the input_checker function smaller and to eliminate repeated code
def three_variables_looper_geometric():
    a = input("enter a1: ")
    b = input("enter r: ")
    c = input("enter n: ")
    original = 0
    count_list = [[a], [b], [c]]
    loop = 0
    for count in count_list:
        if isinstance(count[0], str):
            original = float(eval(count[0]))
            count_list[loop][0] = original
        loop += 1
    return count_list[0][0], count_list[1][0], count_list[2][0]


# loops through all the inputs of a given situation based on whether its arithmetic
# or not, and checks whether the input is string like "6/2" so it could evaluate it, allows input of fractions
def input_checker(arithmetic_1_geometric_2, formula_num, L):
    if arithmetic_1_geometric_2 == 1:
        if formula_num == 1:
            return three_variables_looper_arithmetic()


        elif formula_num == 2:
            return three_variables_looper_arithmetic()

        elif formula_num == 3:
            a1 = input("enter a1: ")
            b = input("enter n: ")
            c = input("enter d: ")
            original = 0
            count_list = [[a1], [b], [c], [L]]
            loop = 0
            for count in count_list:
                if isinstance(count[0], str):
                    original = float(eval(count[0]))
                    count_list[loop][0] = original
            return count_list[0][0], count_list[1][0], count_list[2][0], count_list[3][0]
            
    elif arithmetic_1_geometric_2 == 2:
        if formula_num == 1:
            return three_variables_looper_geometric()
        
        elif formula_num == 2:
            return three_variables_looper_geometric()

        elif formula_num == 3:
            a1 = input("enter a1: ")
            b = input("enter n: ")
            original = 0
            count_list = [[a1], [b]]
            loop = 0
            for count in count_list:
                if isinstance(count[0], str):
                    original = float(eval(count[0]))
                    count_list[loop][0] = original
            return count_list[0][0], count_list[1][0]

# as this code is for a calculator the a and b buttons are right beside each other, so after you find your desired result
# you enter a to stop and b to continue 
def stopper():
    stop_flag = False
    stop_or_continue = ""
    while stop_or_continue != "a" or "b":
        stop_or_continue = input("Stop?: ")
        if stop_or_continue == "a":
            stop_flag = True
            break
        if stop_or_continue == "b":
            stop_flag = False
            break
    if stop_flag:
        raise SystemExit


print("Sequence & Series Solver")

# asks whether you want to solve arithmetically or geometrically, depends on the sequence/series
while True:
    choice = ""
    choices = ["a", "b", "c", "d"]
    while choice not in choices:
        choice = input("a for arithmetic\nb for geometric\n>> ")

    if choice == "a":
        arithmetic_1_geometric_2 = 1
        choice_2 = input("a for a_nth term\nb for sum\n>> ")

        # the variable arithmetic_1_geometric_2 refers to whether the inputs are for arithmetic hence it is 1, or the inputs
        # are for geometric hence 2
        # formula_num refers to the types of formulas you'll use in sequences/series
        if choice_2 == "a":
            print("a_nth=a1+(n-1)d")
            formula_num = 1
            a1, n, d = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = a1+(n-1)*d
            print(result)

        elif choice_2 == "b":
            print("Sn=(n/2)(2a1+(n-1)d)\nSn=(n/2)(a1+L)\nEnter x if L is unknown")
            L = input("Enter L: ")
            if L == "x":
                formula_num = 2
                a1, n, d = input_checker(arithmetic_1_geometric_2, formula_num, L)
                result = (n/2)*(2*a1+(n-1)*d)
                print(result)
            else:
                formula_num = 3
                a1, n, d, L = input_checker(arithmetic_1_geometric_2, formula_num, L)
                result = (n/2)*(a1+L)
                print(result)
    
    elif choice == "b":
        arithmetic_1_geometric_2 = 2
        choice_2 = input("a for a_nth term\nb for sum\nc for sum_to_inf")

        if choice_2 == "a":
            print("a_nth=a1(r)^(n-1)")
            formula_num = 1
            a1, r, n = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = a1*(r)**(n-1)
            print(result)

        elif choice_2 == "b":
            print("Sn=(a1(1-(r)^n))/(1-r)")
            formula_num = 2
            a1, r, n = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = (a1(1-(r)**n))/(1-r)
            print(result)

        elif choice_2 == "c":
            print("S_inf=a1/(1/r)")
            formula_num = 3
            a1, r = input_checker(arithmetic_1_geometric_2, formula_num, 0)
            result = a1/(1-r)
            print(result)
    
    stopper()
```