Why does my Arithmetic Calculator work well on Pycharm but not Replit?

Hi folks!

I am new here and just finished typing my code for the arithmetic calculator project using Pycharm.

The task is given in the link below:

https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter

I built my code in Pycharm and it works for all of the cases I have attempted using Pycharm. However, it does not appear to work in replit.com. I get at least ten errors :frowning: . Below is my code and after are the errors shown. There are a lot, so even helping clarify one would be kindly appreciated. I am just a novice at unit-testing.

My code:

def arithmetic_arranger(problems, answer):

if len(problems) > 5:
    print("Error: Too many problems.")
    quit()
first_operand = list()
op_sign = list()
second_operand = list()

for problem in problems:
    pieces = problem.split()
    first_operand.append(pieces[0])
    op_sign.append(pieces[1])
    second_operand.append(pieces[2])

for sign in op_sign:
    if sign == "*" or sign == "/":
        print("Error: Operator must be '+' or '-'.")
        quit()

for op1 in first_operand:
    if len(op1) > 4:
        print("Error: Numbers cannot be more than four digits.")
        quit()
    try:
        integer = int(op1)
    except:
        print("Error: Numbers must only contain digits.")
        quit()

for op2 in second_operand:
    if len(op2) > 4:
        print("Error: Numbers cannot be more than four digits.")
        quit()
    try:
        integer = int(op2)
    except:
        print("Error: Numbers must only contain digits.")
        quit()


new_str1 = ''  # an empty string to add each of the stringed integers.
str1 = '  '  # a string with two whitespaces for each sign.

# For for first line:
for i in range(len(first_operand)):  # length of first_operand is dependent upon how many numbers the user inputs.
    if len(str(first_operand[i])) < len(str(second_operand[i])):
        numb_of_spaces1 = len(str(second_operand[i])) - len(str(first_operand[i]))
        new_str1 = new_str1 + str1 + ' ' * numb_of_spaces1 + str(first_operand[i]) + ' ' * 4
    else:
        new_str1 = new_str1 + str1 + str(first_operand[i]) + ' ' * 4

# For second line:
new_str2 = ''  # a string with no whitespaces
str2 = ' '  # a string with one whitespace

# For third line, the number of underscores depends on the sign:
new_str3 = ''

for j in range(len(second_operand)):  # length of op_sign is dependent upon how many numbers the user inputs.
    if len(str(first_operand[j])) > len(str(second_operand[j])):
        numb_of_spaces2 = len(str(first_operand[j])) - len(str(second_operand[j]))
        new_str2 = new_str2 + str(op_sign[j]) + str2 + ' ' * numb_of_spaces2 + str(second_operand[j]) + ' ' * 4
        new_str3 = new_str3 + '_' * len(str(op_sign[j]) + str2 + ' ' * numb_of_spaces2 + str(second_operand[j])) + ' ' * 4
    else:
        new_str2 = new_str2 + str(op_sign[j]) + str2 + str(second_operand[j]) + ' ' * 4
        new_str3 = new_str3 + '_' * len(str(op_sign[j]) + str2 + str(second_operand[j])) + ' ' * 4

# If the user wants the answers to their problems

if answer == "True":
    new_str4 = ''  # create an empty string for the answers
    str4 = '  '  # string with two whitespaces
    str4_b = ' '  # string with one whitespace for negative numbers
    for m in range(len(op_sign)):
        if op_sign[m] == '+':
            outcome = int(first_operand[m]) + int(second_operand[m])
            if outcome >= 0 and len(str(outcome)) <= 4:
                new_str4 = new_str4 + str4 + str(outcome) + ' '*4 # adds two spaces for 4 or less digits
            else:
                new_str4 = new_str4 + str4_b + str(outcome) + ' '*4 # adds one white space for 5 digits

        else:
            outcome = int(first_operand[m]) - int(second_operand[m])
            if outcome < 0:
                new_str4 = new_str4 + str4_b + str(outcome) + ' '*4
            elif outcome == 0:
                numb_of_spaces4 = len(str(first_operand[m])) - len(str(outcome))
                new_str4 = new_str4 + str4 + ' ' * numb_of_spaces4 + str(outcome) + ' ' * 4
            else:
                if len(first_operand[m]) == 4 and len(str(outcome)) == 4:
                    new_str4 = new_str4 + str4 + str(outcome) + ' '*4 # four digits for both first operand and outcome
                elif len(first_operand[m]) == 4 and len(str(outcome)) == 3:
                    new_str4 = new_str4 + ' '*3 + str(outcome) + ' '*4 # four digits for first operand and three digits for outcome
                elif len(first_operand[m]) == 4 and len(str(outcome)) == 2:
                    new_str4 = new_str4 + ' '*4 + str(outcome) + ' '*4 # four digits for first operand and two digits for outcome
                elif len(first_operand[m]) == 4 and len(str(outcome)) == 1:
                    new_str4 = new_str4 + ' '*5 + str(outcome) + ' '*4 # four digits for first operand and one digit for outcome
                elif len(first_operand[m]) == 3 and len(str(outcome)) == 3:
                    new_str4 = new_str4 + str4 + str(outcome) + ' '*4 # three digits for first operand and three digits for outcome
                elif len(first_operand[m]) == 3 and len(str(outcome)) == 2:
                    new_str4 = new_str4 + ' '*3 + str(outcome) + ' '*4 # three digits for first operand and two digits for outcome
                elif len(first_operand[m]) == 3 and len(str(outcome)) == 1:
                    new_str4 = new_str4 + ' '*4 + str(outcome) + ' '*4 # three digits for first operand and one digit for outcome
                elif len(first_operand[m]) == 2 and len(str(outcome)) == 2:
                    new_str4 = new_str4 + str4 + str(outcome) + ' '*4 # two digits for first operand and two digits for outcome
                elif len(first_operand[m]) == 2 and len(str(outcome)) == 1:
                    new_str4 = new_str4 + ' '*3 + str(outcome) + ' '*4 # two digits for first operand and one digit for outcome
                else:
                    new_str4 = new_str4 + str4 + str(outcome) + ' '*4 # one digit for first operand and one digit for outcome
            outcome = str(outcome)
            outcome = outcome.lstrip('-')
            if len(outcome) == 5:
                new_str4 = new_str4 + str4_b + outcome + ' ' * 4

    arranged_problems = new_str1 + '\n' + new_str2 + '\n' + new_str3 + '\n' + new_str4
    return arranged_problems
else:
    arranged_problems = new_str1 + '\n' + new_str2 + '\n' + new_str3
    return arranged_problems

First several of the errors:

assert actual == expected_output, fail_message
E AssertionError: Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”]
E assert ’ 3 3… _____ ’ == ’ 3 3…---- -----’
E - 3 3801 45 123
E + 3 3801 45 123
E ? ++++
E - + 855 - 2 + 43 + 49
E + + 855 - 2 + 43 + 49
E ? ++++
E - ----- ------ ---- -----…
E
E …Full output truncated (2 lines hidden), use ‘-vv’ to show

test_module.py:77: AssertionError
________________ test_template[test_five_problems_arrangement] _________________

arguments = [[‘11 + 4’, ‘3801 - 2999’, ‘1 + 2’, ‘123 + 49’, ‘1 - 9380’]]
expected_output = ’ 11 3801 1 123 1\n+ 4 - 2999 + 2 + 49 - 9380\n---- ------ — ----- ------’
fail_message = ‘Expected different output when calling “arithmetic_arranger()” with [“11 + 4”, “3801 - 2999”, “1 + 2”, “123 + 49”, “1 - 9380”]’

@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
  actual = arithmetic_arranger(*arguments)

E TypeError: arithmetic_arranger() missing 1 required positional argument: ‘answer’

test_module.py:76: TypeError
____________________ test_template[test_too_many_problems] _____________________

arguments = [[‘44 + 815’, ‘909 - 2’, ‘45 + 43’, ‘123 + 49’, ‘888 + 40’, ‘653 + 87’]]
expected_output = ‘Error: Too many problems.’
fail_message = ‘Expected calling “arithmetic_arranger()” with more than five problems to return “Error: Too many problems.”’

@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
  actual = arithmetic_arranger(*arguments)

E TypeError: arithmetic_arranger() missing 1 required positional argument: ‘answer’

test_module.py:76: TypeError
____________________ test_template[test_incorrect_operator] ____________________

arguments = [[‘3 / 855’, ‘3801 - 2’, ‘45 + 43’, ‘123 + 49’]]
expected_output = “Error: Operator must be ‘+’ or ‘-’.”
fail_message = ‘Expected calling “arithmetic_arranger()” with a problem that uses the “/” operator to return “Error: Operator must be '+' or '-'.”’

@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
  actual = arithmetic_arranger(*arguments)

E TypeError: arithmetic_arranger() missing 1 required positional argument: ‘answer’

test_module.py:76: TypeError
_____________________ test_template[test_too_many_digits] ______________________

arguments = [[‘24 + 85215’, ‘3801 - 2’, ‘45 + 43’, ‘123 + 49’]]
expected_output = ‘Error: Numbers cannot be more than four digits.’
fail_message = ‘Expected calling “arithmetic_arranger()” with a problem that has a number over 4 digits long to return “Error: Numbers cannot be more than four digits.”’

@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
  actual = arithmetic_arranger(*arguments)

E TypeError: arithmetic_arranger() missing 1 required positional argument: ‘answer’

test_module.py:76: TypeError
_______________________ test_template[test_only_digits] ________________________

arguments = [[‘98 + 3g5’, ‘3801 - 2’, ‘45 + 43’, ‘123 + 49’]]
expected_output = ‘Error: Numbers must only contain digits.’
fail_message = ‘Expected calling “arithmetic_arranger()” with a problem that contains a letter character in the number to return “Error: Numbers must only contain digits.”’

@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
  actual = arithmetic_arranger(*arguments)

E TypeError: arithmetic_arranger() missing 1 required positional argument: ‘answer’

test_module.py:76: TypeError
_______________ test_template[test_two_problems_with_solutions] ________________

arguments = [[‘3 + 855’, ‘988 + 40’], True]
expected_output = ’ 3 988\n+ 855 + 40\n----- -----\n 858 1028’
fail_message = ‘Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with [“3 + 855”, “988 + 40”] and a second argument of True.’

@pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
def test_template(arguments, expected_output, fail_message):
    actual = arithmetic_arranger(*arguments)
  assert actual == expected_output, fail_message

E AssertionError: Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with [“3 + 855”, “988 + 40”] and a second argument of True.
E assert ’ 3 9… _____ ’ == ’ 3 9… 858 1028’
E - 3 988
E + 3 988
E ? ++++
E - + 855 + 40
E + + 855 + 40
E ? ++++
E + _____ _____ …
E
E …Full output truncated (3 lines hidden), use ‘-vv’ to show

test_module.py:77: AssertionError
_______________ test_template[test_five_problems_with_solutions] _______________

arguments = [[‘32 - 698’, ‘1 - 3801’, ‘45 + 43’, ‘123 + 49’, ‘988 + 40’], True]
expected_output = ’ 32 1 45 123 988\n- 698 - 3801 + 43 + 49 + 40\n----- ------ ---- ----- -----\n -666 -3800 88 172 1028’
fail_message = ‘Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with five arithmetic problems and a second argument of True.’

Next time please check the formatting of your post - it’s kinda hard to read with only parts of it getting rendered as code.

Anyway, given the errors look like the standard issues in this project, it’s not the biggest issue.

  1. the “answer” argument to the function should be optional, meaning giving it a default value.
  2. you are supposed to “return” error messages, not print-and-quit.
  3. while it is hard to read because of the lacking format, I assume you got the usual 4 trailing spaces at the end of your lines (indicated by the ? ++++ in the assertion error, which if correctly formatted would actually line up with the displayed outputs).

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.