[2017-09-11] Challenge #331 [Easy] The Adding Calculator

Python 3.6.2

This took me ages! I am so happy I finally got it finished! Btw, I used negative numbers a few times in the code as nothing said I couldn't.

` import sys

TAB = "\t" NEW_LINE = "\n" TITLE_STYLER = "-"

ADDITION = "+" SUBSTRACTION = "-" MULTIPLICATION = "*" DIVISION = "/" EXPONENT = ""

ALLOWED_CHARACTERS = [ADDITION, SUBSTRACTION] ALLOWED_CHARACTERS_EXPENDED = ALLOWED_CHARACTERS + [MULTIPLICATION, DIVISION, EXPONENT, " "]

error_count = 0

def is_int(item=None): if item is None: return False try: int(item) return True except ValueError: return False

def is_negative(item=None): if item is None: return False elif item < 0: return True return False

def error(objects, sep=" ", end="\n", flush=False): global error_count error_count += 1 print(objects, sep=" ", end=end, file=sys.stderr, flush=flush)

def add(a, b): return a + b

def substract(a, b): return a + -b

def multiply(a, b): result = 0 result_negative = False

if is_negative(a) and is_negative(b):
    pass

elif is_negative(a) or is_negative(b):
    result_negative = True

a = abs(a)
b = abs(b)

if a < b:
    loop_times = a
    loop_amount = b
else:
    loop_times = b
    loop_amount = a

for _ in range(loop_times):
    result += loop_amount

if result_negative:
    return -result

return result

def divide(a, b): result = 0 result_negative = False

if b == 0:
    return "Not-defined"

if is_negative(a) and is_negative(b):
    pass

elif is_negative(a) or is_negative(b):
    result_negative = True

a = abs(a)
b = abs(b)

for _ in range(0, a, b):
    result += 1

if a != multiply(result, b):
    return "Non-integral answer"

if result_negative:
    return -result

return result

def power(a, b): result = 1 result_negative = False

if is_negative(b):
    return "Non-integral answer"

for counter in range(b):
    result = multiply(result, a)

return result

print(6 * TAB + TITLE_STYLER * 3 + " Adding Calculator " + TITLE_STYLER * 3) print(3 * TAB + "(addition '{}', substraction '{}', multiplication '{}', division '{}', exponent '{}')" .format(ADDITION, SUBSTRACTION, MULTIPLICATION, DIVISION, EXPONENT) + NEW_LINE * 2)

while True: number_1, number_2, operator = "", "", "" number_1_complete, number_2_complete, operator_complete = False, False, False error_count, result = 0, 0

operation = input(">> ")

for counter, character in enumerate(list(operation)):
    COUNTER_CHECK_1 = counter == 0
    COUNTER_CHECK_2 = counter > 0

    CHARACTER_NOT_SPACE_CHECK = character != " "

    if (is_int(character) and COUNTER_CHECK_1) or (character in ALLOWED_CHARACTERS and COUNTER_CHECK_1):
        number_1 += character

    elif (is_int(character) and COUNTER_CHECK_2) or (character in ALLOWED_CHARACTERS_EXPENDED and COUNTER_CHECK_2):
        if not CHARACTER_NOT_SPACE_CHECK and operator_complete and is_int(number_2):
            number_2_complete = True

        if is_int(character) or (operator_complete and CHARACTER_NOT_SPACE_CHECK):

            if number_1_complete and not number_2_complete and operator_complete:
                number_2 += character

            elif (number_2_complete and is_int(number_2)) or number_1_complete:
                error("ERROR 1: Too many integers and or operators!")
                break

            else:
                number_1 += character

        elif character in ALLOWED_CHARACTERS_EXPENDED:
            number_1_complete = True

            if CHARACTER_NOT_SPACE_CHECK:
                operator += character
                operator_complete = True

    else:
        if COUNTER_CHECK_1:
            error("ERROR 2: The first character can't be '{}'!".format(character))
            break

        else:
            error("ERROR 3: Character '{}', is invalid!".format(character))
            break

for character in number_2:
    if error_count:
        break

    else:
        if not is_int(character) and character not in ALLOWED_CHARACTERS:
            error("ERROR 4: Invalid character for the second integer: '{}'!".format(character))
            break

        elif not is_int(number_2):
            error("ERROR 5: Invalid operation!")
            break

if error_count:
    continue

elif not number_2 or not operator:
    print(number_1)
    continue

number_1 = int(number_1)
number_2 = int(number_2)

if operator == ADDITION:
    result = add(number_1, number_2)
    print(result)

elif operator == SUBSTRACTION:
    result = substract(number_1, number_2)
    print(result)

elif operator == MULTIPLICATION:
    result = multiply(number_1, number_2)
    print(result)

elif operator == DIVISION:
    result = divide(number_1, number_2)
    print(result)

elif operator == EXPONENT:
    result = power(number_1, number_2)
    print(result)

` I am a beginner programmer so please let me know how I could improve the solution!

/r/dailyprogrammer Thread