Printing list of string side by side

I am working on the first assessment of Scientific Computing with Python and I have managed to format the strings as required by the question but now I am facing the issue with the O/P. I am not able to print the list contents side by side. I have formatted the provided list of strings and stored it inside a list as [' 1\n+ 1\n---\n 2', ' 2\n+ 22\n----\n 24', ' 3\n+ 333\n-----\n 336', ' 4\n+ 4444\n------\n 4448']. I tried to O/P the strings using the ' '.join(formatted_strings) but it doesn’t seem to work. I would be more than grateful if anyone can help me with this. Thanks

Please post your current code - Thanks

Here’s the whole code for your reference.

def arithmetic_formatter(problems, calc=False):
    results = []
    calculated = ""

    if len(problems) > 5:
        return "Error: Too many problems."

    for problem in problems:
        if not check_valid_operators(problem):
            return "Error: Operators must be '+' or '-'."
        elif not check_valid_operands(problem):
            return "Error: Numbers must only contain digits."
        elif not check_operand_valid_length(problem):
            return "Error: Numbers cannot be more than four digits."

        if calc:
            calculated = str(
                solve(
                    int(problem.split(" ")[0]),
                    int(problem.split(" ")[2]),
                    problem.split(" ")[1],
                )
            )
            results.append(format_problem_string(problem, calculated))
        else:
            results.append(format_problem_string(problem))

    return results


def format_problem_string(problem_string, calculated=""):
    problem_split_list = problem_string.split(" ")
    formatted_string = ""
    upper_spaces, lower_spaces, result_spaces, dashes = 0, 0, 0, 0

    if len(problem_split_list[0]) >= len(problem_split_list[2]):
        upper_spaces = 2
        dashes = len(problem_split_list[0]) + 2
        lower_spaces = dashes - len(problem_split_list[2]) - 1

        if len(calculated) == dashes:
            result_spaces = 0
        elif len(calculated) < dashes:
            result_spaces = dashes - len(calculated)

        formatted_string = f"{(' ' * upper_spaces)}{problem_split_list[0]}\n{problem_split_list[1]}{(' ' * lower_spaces)}{problem_split_list[2]}\n{('-' * dashes)}\n{(' ' * result_spaces)}{calculated}"
    else:
        dashes = len(problem_split_list[2]) + 2
        lower_spaces = dashes - len(problem_split_list[2]) - 1
        upper_spaces = dashes - len(problem_split_list[0])

        if len(calculated) == dashes:
            result_spaces = 0
        elif len(calculated) < dashes:
            result_spaces = dashes - len(calculated)

        formatted_string = f"{(' ' * upper_spaces)}{problem_split_list[0]}\n{problem_split_list[1]}{(' ' * lower_spaces)}{problem_split_list[2]}\n{('-' * dashes)}\n{(' ' * result_spaces)}{calculated}"

    return formatted_string


def solve(num1, num2, operand):
    if operand == "+":
        return num1 + num2
    elif operand == "-":
        return num1 - num2


def check_valid_operators(problem):
    return problem.find("+") != -1 or problem.find("-") != -1


def check_valid_operands(problem):
    return problem.split(" ")[0].isdigit() and problem.split(" ")[2].isdigit()


def check_operand_valid_length(problem):
    return len(problem.split(" ")[0]) <= 4 and len(problem.split(" ")[2]) <= 4


print(arithmetic_formatter(["1 + 1", "2 + 22", "3 + 333", "4 + 4444"], True))

Generally speaking:

You should call the arithmetic_arranger function from main.py and call test data from main.py as well. You can see a test call there already:

print(arithmetic_arranger(['3801 - 2', '123 + 49']))

For that to work you need to call the function arithmetic_arranger so that the import works:
from arithmetic_arranger import arithmetic_arranger

Don’t test from within arithmetic_arranger.py as you do here:
print(arithmetic_formatter(["1 + 1", "2 + 22", "3 + 333", "4 + 4444"], True))

Start by renaming your function back to arithmetic_arranger (not formatter) or none of the tests are going to work and you won’t be able to complete this.

The tests will also give you valuable feedback on how to structure your output.

Once that’s done, this might be useful for you:

An assertion error gives you a lot of information to track down a problem. For example:

AssertionError: 'Year' != 'Years'
- Year
+ Years
?     +

Your output comes first, and the output that the test expects is second.

AssertionError: ‘Year’ != ‘Years’

Your output: Year does not equal what’s expected: Years

- Year
+ Years
?     +

- Dash indicates the incorrect output
+ Plus shows what it should be
? The Question mark line indicates the place of the character that’s different between the two lines. Here a + is placed under the missing s .

Here’s another example:

E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E       assert '  3801      123    \n   - 2     + 49    \n------    -----    \n' == '  3801      123\n-    2    +  49\n------    -----'
E         -   3801      123
E         +   3801      123    
E         ?                ++++
E         - -    2    +  49
E         +    - 2     + 49    
E         - ------    -----
E         + ------    -----    
E         ?                +++++

The first line is long, and it helps to view it as 2 lines in fixed width characters, so you can compare it character by character:

'  3801      123    \n   - 2     + 49    \n------    -----    \n'
'  3801      123\n-    2    +  49\n------    -----'

Again, your output is first and the expected output is second. Here it’s easy to see extra spaces or \n characters.

E         -   3801      123
E         +   3801      123    
E         ?                ++++

Here the ? line indicates 4 extra spaces at the end of a line using four + symbols. Spaces are a little difficult to see this way, so it’s useful to use both formats together.

I hope this helps interpret your error!

I don’t think you will be able to “print a list of strings side by side” as you put it. Rather you need to break them up and think “line by line” like a dot matrix printer.

Your first line will be the the 1st element of each problem:

`      1              2               3        `

Your second line will be the operator and 2nd element:

     `+ 1           + 22           +33`

and the last line:

--------------   ---------  -----------

etc.

1 Like

Hey @pkdvalis. This is a problem from my side as I don’t explain my code the whole code here is written locally on my machine not in reply that is why it is really different from how it should be regarding your first suggestion on code I have taken care of that I just had the issue with side by side formatting of my code but thanks for taking out time to go through my whole code and explaining all things in details. I appreciate it.

1 Like

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