Scientific Computing with Python Projects - Arithmetic Formatter - Formatting problem

Tell us what’s happening:
Hi!

Having some problems with running my code on repl. The traceback is saying that the output string is not in the correct format. I have been unable to pinpoint the cause of this. Everything looks good to me. Please let me know if you can find an issue.

Your code so far

import re

def arithmetic_arranger(problems, shower=False):
    if len(problems) > 5:
        return 'Error: Too many problems.'

    if any('*' in s for s in problems) or any('/' in s for s in problems):
        return 'Error: Operator must be \'+\' or \'-\'.'

    for item in problems:
        if re.search('[A-Za-z]', item):
            return 'Error: Numbers must only contain digits.'

    for item in problems:
        korv = item.split()
        for i in korv:
            if len(i) > 4:
                return 'Error: Numbers cannot be more than four digits.'

    janne = ''
    top_row = ''
    middle_row = ''
    bottom_row = ''
    sum_row = ''
    for item in problems:

        lst = item.split()
        mark = '-'
        if len(lst[0]) > len(lst[2]):
            line = lst[0].rjust(len(lst[0]) + 2) + '\n' + lst[1] + lst[
                2].rjust(len(lst[0]) + 1) + '\n' + mark.rjust(
                    len(lst[0]) + 2, '-')
            janne = janne + line
            top_row += lst[0].rjust(len(lst[0]) + 2) + '  '
            middle_row += lst[1] + lst[2].rjust(len(lst[0]) + 1) + '  '
            bottom_row += mark.rjust(len(lst[0]) + 2, '-') + '  '
            if lst[1] == '-':
                sum_row += str(int(lst[0]) -
                               int(lst[2])).rjust(len(lst[0]) + 2) + '  '
            else:
                sum_row += str(int(lst[0]) +
                               int(lst[2])).rjust(len(lst[0]) + 2) + '  '
        elif len(lst[0]) == len(lst[2]):
            line = lst[0].rjust(len(lst[0]) + 2) + '\n' + lst[1] + lst[
                2].rjust(len(lst[0]) + 1) + '\n' + mark.rjust(
                    len(lst[0]) + 2, '-')
            top_row += lst[0].rjust(len(lst[0]) + 2) + '  '
            middle_row += lst[1] + lst[2].rjust(len(lst[0]) + 1) + '  '
            bottom_row += mark.rjust(len(lst[0]) + 2, '-') + '  '
            janne = janne + line
            if lst[1] == '-':
                sum_row += str(int(lst[0]) -
                               int(lst[2])).rjust(len(lst[0]) + 2) + '  '
            else:
                sum_row += str(int(lst[0]) +
                               int(lst[2])).rjust(len(lst[0]) + 2) + '  '
        else:
            line = lst[0].rjust(len(lst[2]) + 2) + '\n' + lst[1] + lst[
                2].rjust(len(lst[2]) + 1) + '\n' + mark.rjust(
                    len(lst[2]) + 2, '-')
            janne = janne + line
            top_row += lst[0].rjust(len(lst[2]) + 2) + '  '
            middle_row += lst[1] + lst[2].rjust(len(lst[2]) + 1) + '  '
            bottom_row += mark.rjust(len(lst[2]) + 2, '-') + '  '
            if lst[1] == '-':
                sum_row += str(int(lst[0]) -
                               int(lst[2])).rjust(len(lst[2]) + 2) + '  '
            else:
                sum_row += str(int(lst[0]) +
                               int(lst[2])).rjust(len(lst[2]) + 2) + '  '
    if shower:
        return top_row.rstrip() + '\n' + middle_row.rstrip() + '\n' + bottom_row.rstrip() + '\n' + sum_row.rstrip()
    else:
        return top_row.rstrip() + '\n' + middle_row.rstrip() + '\n' + bottom_row.rstrip()

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36.

Challenge: Arithmetic Formatter

Link to the challenge:

Can you provide a link to your repl?

What do the failing tests say?


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hello Jeremy!

Sorry for the delay, I’ve cleaned up my code and restarted repl.

Yes, here’s the link: https://repl.it/@zealti/boilerplate-arithmetic-formatter-4#arithmetic_arranger.py (hope this works)

The failing tests says:
Error 1: ‘Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”]’

Error 2: Error 1: ‘Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithemetic problems and a second argument of True.’

Thank you for taking the time.

It looks like you are only capturing the first problem in your output, not all of the problems passed in?

image

Yes, it looks like it’s only accepting the first problem for both of them. I’ve ran the same code in pycharm with the same list as input. However, there I’m getting the same string as the expected string.

Just realised my last few lines with the return string was indented in the for loop.

1 Like

Ah, your indentation on your return on repl places it inside of the loop over problems.

Note to self: Set pycharm indentation to 2 spaces. The last problem was my parsing between the problems.

Getting “OK” as result now. Well, that’s what coding does a friday night. Thank you so much for the help Jeremy!

1 Like