Help with arithmetic formatter

Tell us what’s happening:
@ieahleen this is probably the one millionth time someone is asking for this kind of help. but i’ve been stuck on trying to arrange the problems right but i keep getting errors, the link to my code is above, maybe you’ll see something i’m not, thank you for taking time to check it out :kissing_heart:

LINK: https://repl.it/@OyeyiolaMaryam/boilerplate-arithmetic-formatter-2#arithmetic_arranger.py

Your code so far
def arithmetic_arranger(problems, solution=False):

line1 = ''
line2 = ''
line3 = ''
line4 = ''

if len(problems) > 5:
   return "Error: Too many problems."
for problem in problems:
    pieces = problem.split()
    num1 = pieces[0]
    op = pieces[1]
    num2 = pieces[2]
     
    
    if not ((op == '+') or (op == '-')):
        return "Error: Operator must be '+' or '-'."
    if (len(num1) > 4) or (len(num2) > 4):
        return "Error: Numbers cannot be more than four digits."
    if not (num1.isnumeric()) or (num2.isnumeric()):
        return "Error: Numbers must only contain digits."
   
    #total = 0
    if (op == '+'): solution = num1 + num2
    elif (op == '-'): solution = num1 - num2

    
    width = max(len(num1), len(num2)) + 2
    space = '    '

    
    line1 = str(num1).rjust((len(num2) - len(num1)) + 2) + space
    line2 = op + str(num2).rjust((len(num1) - len(num2)) + 1) + space
    line3 = str('-' * (width)) + space
    line4 = str(solution).rjust(width) + space

    if solution == True:
        arranged_problems = line1 +'\n' + line2 + '\n' + line3 + '\n' + line4
    else:
        arranged_problems = line1 +'\n' + line2 + '\n' + line3
return arranged_problems

Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

The error is on line 21.

Error: Numbers must only contain digits.

Try printing the type of num1 and num2. They should be numbers but they are not.

1 Like

It’s not working. Still getting the same error.

try to substitute True or False there and see if you get the result you want

not (num1.isnumeric()) or (num2.isnumeric())
print(not (True) or (True))
print(not (True) or (False))
print(not (False) or (True))
print(not (False) or (False))

the result of True is when it executes (returns the error) and False when it doesn’t, so you want False for first case and True for all the other

1 Like

I see what you mean now. Thank you so much

a note: it’s correct and expected that num1 and num2 are strings, you got them breaking apart a string, and are treating them as string, if you convert them to integers you get an error at each step - you need them as numbers only if you need the solution to the operations

Thank you so much! I did it, no more Failures, just errors on line 26, “unsupported operand type(s)”
Tried casting it to int, but it didn’t work

Wherever you are using len() Make sure num1 and num2 are strings and wherever you are using arithmetic operators make sure that these are integers. At the beginning of all this both are strings. I have executed the code in my editor and it is fine. Just make sure you aren’t missing any type casting in between.

how did you try to cast to int?

Here;

if (op == ‘+’): solution = int(num1) + int(num2)
elif (op == ‘-’): solution = int(num1) - int(num2)