import sys


def naive_calculator(operator, number_list):
    if operator == "+":
        calculated_output = sum(number_list)

    elif operator == "*":
        calculated_output = 1
        for i in number_list:
            calculated_output *= i

    elif operator == "^":
        calculated_output = number_list[0]
        for i in number_list[1:]:
            calculated_output = calculated_output ** i
    else:
        sys.exit("Error: operator not recognised")

    return calculated_output


def string_to_list_of_ints(string):
    number_list = string.split()
    number_list = [int(i) for i in number_list]
    return number_list


def get_input():
    initial_input = input("Please input an expression:\n")
    if "q" in initial_input.lower():
        sys.exit("Quiting...")
    return initial_input


def find_most_nested_expression(string):
    index_2 = string.index(")")
    index_1 = string.rindex("(", 0, index_2)
    return string[index_1 + 1:index_2]


def chew_through_nests(string):
    #print(string)

    if ")" in string:
        most_nested = find_most_nested_expression(string)
        operator = most_nested[0]
        number_list = string_to_list_of_ints(most_nested[1:])
        string = string.replace("(" + most_nested + ")", str(naive_calculator(operator, number_list)))
        return chew_through_nests(string)
    else:
        return string


def check_input(initial_input):
    if initial_input.count("(") == initial_input.count(")") and initial_input.count("(") > 0:
        return True
    else:
        sys.exit("Malformed input. Check your brackets.")


def main():
    """ This is a rudimentary prefix calculator """
    print("This is a rudimentary prefix calculator. The first 3 goes are free!\nType \"q\" to quit.")
    goes = 0
    while goes < 3:
        initial_input = get_input()
        #initial_input = "(+ 2 (^ 3 3) (* 3 2))"

        check_input(initial_input)
        answer = chew_through_nests(initial_input)

        print("= " + answer)
        goes += 1

if __name__ == "__main__":
    main()
@@ -1,14 +1,54 @@
-import sys
+#! /usr/bin/env python3%0Aimport sys%0Aimport unittest
 %0A%0A%0Ad
@@ -1633,104 +1633,1085 @@
 )%0A%0A%0A
-def main():%0A    %22%22%22 This is a rudimentary prefix calculator %22%22%22%0A    print(%22This is a rudimentary
+class MyCalculatorTests(unittest.TestCase):%0A    def test_find_most_nested_expression(self):%0A        test_expression = %22(+ 3 (%5E 2 4)%22%0A        self.assertEqual(find_most_nested_expression(test_expression), %22%5E 2 4%22)%0A%0A    def test_string_to_list_of_ints(self):%0A        test_string = %223 2 4 5%22%0A        self.assertEqual(string_to_list_of_ints(test_string), %5B3, 2, 4, 5%5D)%0A%0A    def test_naive_calculator(self):%0A        test_number_list = %5B1, 3, 4, 5%5D%0A        test_non_ints_and_negatives = %5B-2, 1, 0.5, 7.6%5D%0A        test_complex_number_list = %5B-1, 0.5%5D%0A        self.assertEqual(naive_calculator(%22*%22, test_number_list), 60)%0A        self.assertEqual(naive_calculator(%22+%22, test_number_list), 13)%0A        self.assertEqual(naive_calculator(%22%5E%22, test_number_list), 1)%0A        self.assertEqual(naive_calculator(%22+%22, test_non_ints_and_negatives), 7.1)%0A        self.assertEqual(naive_calculator(%22*%22, test_non_ints_and_negatives), -7.6)%0A        self.assertAlmostEqual(naive_calculator(%22%5E%22, test_complex_number_list), 0+1j)%0A%0Adef main():%0A    %22%22%22 This is a prefix calculator %22%22%22%0A    print(%22This is a
  pre

add unittests