Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
Describe your issue in detail here.
my code runs and prints the code properly formatted in VSCode but when I copy and paste it into replit it fails every test. I created a main.py that imports and calls the function with similar parameters as what is on replit. I am coding in VSCode so I can work offline and save my work.

Your code so far
def arithmetic_arranger(*args):

for problems in args:
    print(problems[0])

    if error_checking(problems[0]) == True:
    
        arranged_problems = format_problems(*args)
        return arranged_problems

#check for errors

def error_checking(problems):
#check for 5 or less problems
print(problems)

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

#loop through problems
    
for problem in problems:
    problem = problem.split()
    #print(problem)
    for x in problem:

        #check for operands containing more than 4 digits
        if len(x) > 4:
            return "Error: Too many digits"
        #is digit test
        isdigit = "0123456789"
        for digit in problem[0]:
            if digit not in isdigit:
                return "Error: Operand must only contain digits."
        for digit in problem[2]:
            if digit not in isdigit:
                return "Error: Operand must only contain digits."
        #operator test
        operator = "+-"
        for digit in problem[1]:
            if digit not in operator:
                return "Error: Only additiion and subtrtaction allowed."
return True

def format_problems(*args):
problems = args[0]
line_one =
line_two =
line_three =
line_four =
padding = " "
for problem in problems[0]:
problem = problem.split()
if len(problem[0]) > len(problem[2]):
width = len(problem[0]) + 2
else:
width = len(problem[2]) + 2
w_one = width - len(problem[0])
w_two = width - len(problem[2]) -1

    line_one.append((w_one * " ") + problem[0])
    line_one.append("    ")

    line_two.append(problem[1])
    line_two.append((w_two * " ") + problem[2])
    line_two.append("    ")

    line_three.append(width * "-")
    line_three.append("    ")


    
    #solve the problems
    if problem[1] == "+":
        result = str(int(problem[0]) + int(problem[2]))
    else:
        result = str(int(problem[0]) - int(problem[2]))
    line_four.append((width - len(result)) * " ")
    line_four.append(result)
    line_four.append("    ")

#concatenate the final string to return    
line_one[-1] = "\n"
line_two[-1] = "\n"
line_three[-1] = "\n"
line_four[-1] = "\n"
answered = ''.join(line_one + line_two + line_three + line_four)
no_answer = ''.join(line_one + line_two + line_three)

if len(*args) > 1:
    return answered
else:
    return no_answer

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

please share a link to the repl.it

repl.it link ← mod edit

I moved the link into a hyperlink tag because it wasn’t displaying right all alone.
It seems that you have an unexpected character at the end of your return value

>   ???
E   AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.
E   assert ('   32         1      45      123      988\n'\n '- 698    - 3801    + 43    +  49    +  40\n'\n '-----    ------    ----    -----    -----\n'\n ' -666     -3800      88      172     1028\n') == ('   32         1      45      123      988\n'\n '- 698    - 3801    + 43    +  49    +  40\n'\n '-----    ------    ----    -----    -----\n'\n ' -666     -3800      88      172     1028')
E          32         1      45      123      988
E       - 698    - 3801    + 43    +  49    +  40
E       -----    ------    ----    -----    -----
E     -  -666     -3800      88      172     1028
E     +  -666     -3800      88      172     1028
E     ?                                          +

I believe that last line with the ? at the start is saying you have some unexpected character there.

I am new to using this platform does this help?

i also tried to test your code manually (i forked it) and it doesn’t return the expected result when someone uses a non-digit character.
(there are also some other issues with error handling such as with someone passing too many problems)

I figured out the extra character issue. I was assigning a new line character to the fourth line in place of the padding. I replaced it with “” and that fixed two of the tests. next I’ll work on the error_checking function

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