Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
Hello everyone! It took my around 2 hours to make this code, I tried all the tests manually on my code and the arithmatic problems are all formatted correctly and if i add True as the second argument the answers show up, but replit says I failed the 10 tests for some reason, I prefer to type my code in vscode editor the same way I did with the javascript course so is there a solution to this or what?

Your code so far

import sys
def arithmatic_arranger(lst, reveal=False):
    ind = 0
    l1 = [ ]
    l2 = []
    l3 = []
    l4 = []
    l5 = []
    l6 = []
    if len(lst) > 5:
        print('Error: Too many problems.')
        sys.exit()
    for i in lst:
        kok = i.split()
        if kok[1] != '+' and kok[1] != '-':
            print('Error: Operator must be', '\'+\'', 'or', '\'-\'')
            sys.exit()
        if len(kok[0]) > 4 or len(kok[2]) > 4:
            print('Error: Numbers cannot be more than four digits.')
            sys.exit()
        if len(kok[0]) > len(kok[2]):
            ind = len(kok[0])+1
        elif len(kok[2]) > len(kok[0]):
            ind = len(kok[2])+1
        elif len(kok[0]) == len(kok[2]):
            ind = len(kok[2])+1
        l1.append(kok[0])
        l2.append(kok[1])
        l3.append(kok[2])
        l4.append(ind+1-len(kok[0]))
        l5.append(ind+1-(len(kok[2])+1))
        try:
            l6.append(eval(kok[0]+kok[1]+kok[2]))
        except SyntaxError:
            print('Error: Numbers must only contain digits.')
            sys.exit()
    tot = len(lst)
    for a in range(tot):
        print(' '*l4[a]+l1[a]+'    ', end='')
    print()
    for b in range(tot):
        print(l2[b]+' '*l5[b]+l3[b]+'    ', end='')
    print()
    for c in range(tot):
        print('-'*(l4[c]+len(l1[c]))+'    ', end='')
    print()
    if reveal == True:
        for d in range(tot):
            print(' '*(l4[d]+len(l1[d])-len(str(l6[d]))) +
                  str(l6[d])+'    ', end='')

Your browser information:

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

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

this is what appears after I run the code on replit

1 Like

I’ve edited your code 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 (').

if you post the errors (or one of them) we can help you interpret…

Tell us what’s happening:
Hello everyone! It took my around 2 hours to make this code, I tried all the tests manually on my code and the arithmatic problems are all formatted correctly and if i add True as the second argument the answers show up, but replit says I failed the 10 tests for some reason, I prefer to type my code in vscode editor the same way I did with the javascript course so is there a solution to this or what?

Your code so far

import sys
def arithmatic_arranger(lst, reveal=False):
    ind = 0
    l1 = list()
    l2 = list()
    l3 = list()
    l4 = list()
    l5 = list()
    l6 = list()
    if len(lst) > 5:
        print('Error: Too many problems.')
        sys.exit()
    for i in lst:
        kok = i.split()
        if kok[1] != '+' and kok[1] != '-':
            print('Error: Operator must be', '\'+\'', 'or', '\'-\'')
            sys.exit()
        if len(kok[0]) > 4 or len(kok[2]) > 4:
            print('Error: Numbers cannot be more than four digits.')
            sys.exit()
        if len(kok[0]) > len(kok[2]):
            ind = len(kok[0])+1
        elif len(kok[2]) > len(kok[0]):
            ind = len(kok[2])+1
        elif len(kok[0]) == len(kok[2]):
            ind = len(kok[2])+1
        l1.append(kok[0])
        l2.append(kok[1])
        l3.append(kok[2])
        l4.append(ind+1-len(kok[0]))
        l5.append(ind+1-(len(kok[2])+1))
        try:
            l6.append(eval(kok[0]+kok[1]+kok[2]))
        except SyntaxError:
            print('Error: Numbers must only contain digits.')
            sys.exit()
    tot = len(lst)
    for a in range(tot):
        print(' '*l4[a]+l1[a]+'    ', end='')
    print()
    for b in range(tot):
        print(l2[b]+' '*l5[b]+l3[b]+'    ', end='')
    print()
    for c in range(tot):
        print('-'*(l4[c]+len(l1[c]))+'    ', end='')
    print()
    if reveal == True:
        for d in range(tot):
            print(' '*(l4[d]+len(l1[d])-len(str(l6[d]))) +
                  str(l6[d])+'    ', end='')

Your browser information:

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

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

this is the errors message

The instructions don’t tell you to do this.

The instructions tell you to return the string with the error message, not print it.


This needs to be fixed everywhere you use this pattern. The instructions want you to always return a string from your function. You should not print and you shouldn’t crash the program inside of your function.

I changed the all the print patterns to return but I only passed 4 of the tests and I still dont know where the problem is, I used the join method to concatenate the (ans) list and the output is correct yet replit still gives me 6 errors so I changed the code and made a for loop to concatenate all the (ans) list items into (str_ans) string and the output is still correct yet replit still gives me the same 6 errors

import sys

sus = list()


def arithmetic_arranger(lst, reveal=False):
    ind = 0
    l1 = list()
    l2 = list()
    l3 = list()
    l4 = list()
    l5 = list()
    l6 = list()
    ans = list()
    str_ans = ''
    if len(lst) > 5:
        return 'Error: Too many problems.'
    for i in lst:
        kok = i.split()
        if kok[1] != '+' and kok[1] != '-':
            return 'Error: Operator must be \'+\' or \'-\'.'
        if len(kok[0]) > 4 or len(kok[2]) > 4:
            return 'Error: Numbers cannot be more than four digits.'
        if len(kok[0]) > len(kok[2]):
            ind = len(kok[0])+1
        elif len(kok[2]) > len(kok[0]):
            ind = len(kok[2])+1
        elif len(kok[0]) == len(kok[2]):
            ind = len(kok[2])+1
        l1.append(kok[0])
        l2.append(kok[1])
        l3.append(kok[2])
        l4.append(ind+1-len(kok[0]))
        l5.append(ind+1-(len(kok[2])+1))
        try:
            l6.append(eval(kok[0]+kok[1]+kok[2]))
        except SyntaxError:
            return 'Error: Numbers must only contain digits.'
    tot = len(lst)
    for a in range(tot):
        ans.append(' '*l4[a]+l1[a]+'    ')
    ans.append('\n')
    for b in range(tot):
        ans.append(l2[b]+' '*l5[b]+l3[b]+'    ')
    ans.append('\n')
    for c in range(tot):
        ans.append('-'*(l4[c]+len(l1[c]))+'    ')
    ans.append('\n')
    if reveal == True:
        for d in range(tot):
            ans.append(
                ' '*(l4[d]+len(l1[d])-len(str(l6[d]))) + str(l6[d])+'    ')
    for i in ans:
        str_ans += i
    return str_ans

I passed the 4 tests that required the function to return an error message

nvm, apparently there were extra spaces that were created after each of my for loops so I had to change them and I passed the tests finally


sorry for duplicating the post, do I post the solution here or is it illegal?

We prefer you didn’t post solutions here unless you want feedback on them.

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