Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

When I try to call my function, it returns me these two errors. I think the cause is the .split() not working right, but if anyone can explain to my why or knows why it actually isn’t working and could explain to me, it would be greatly appreciated. Also, I know my code is rough and things probably aren’t where they should be, I’m new to coding and I was going to troubleshoot through everything, but I just can’t understand why this won’t work.

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        print('Error: Too many problems')
    for problem in problems:
        split = problem.split()
        num1 = split[0]
        operator = split[1]
        num2 = split[2]
        if not operator == '+' or not operator == '-':
            print("Error: Operator must be '+' or '-'.")
        if not num1.isdigit() or not num2.isdigit():
            print('Error: Numbers must only contain digits.')
        if len(num1) or len(num2) > 4:
            print('Error: Numbers cannot be more than four digits')
        if operator == '+':
            answer = int(num1) + int(num2)
        if operator == '-':
            answer = int(num1) + int(num2)
        maximum = max(len(num1),len(num2))
        upperbound = maximum + 2
        lowerbound = len(num2) + 1
        def line_1(num1):
            line1 += (' ' * (upperbound - len(num1))) + int(num1) + '    '
            print(line1)
        def line_2(num2):
            line2 += operator + (' ' * (upperbound - len(num2) - 1)) + int(num2) + '    '
            print(line2)
        def line_3(upperbound):
            line3 = ('-' * upperbound) + '    '
            print(line3)
        def line_4(answer):
            line4 = ((' ' * (upperbound - len(answer))) + int(answer)) + '    '
            print(line4)    

arithmetic_arranger(["32 + 698"])

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Welcome to the forum @landonw0424

Error message mentioned in the instructions.

‘Error: Numbers cannot be more than four digits.’

Your code, what is missing?

Try using the examples given in the test cases.

Happy coding

Thank you, I should have been more specific in my question. I understand that the errors (edit. I just realized that the errors didn’t show, the errors that are printing are the ones for the operator and number length) being printed are from my code, and I understand the reason that it is being printed, but what I don’t understand is why that reason is happening because from my understanding, the code should essentially produce something like this, I’m using the # to show what I believe would be the result:

split = problem.split() # [“32”, “+”, “698”]
num1 = split[0] # “32”
operator = split[1] #“+”
num2 = split[2] #“698”

And so by this, I’m not understanding why the errors are being printed if the strings for both the numbers and operator would pass the if statements. Also I added an else statement to see if that changed anything and it didn’t.

if/or doesn’t quite work this way. What you’ve written translates as: “If len(num1) is true, or len(num2) > 4 is true, then:”

You have two expressions len(num1) and len(num2) > 4.

if ‘num1’ is a number then len(num1) will ALWAYS be true (non-zero).

This is an equivalent:

a = 1

if a or a > 4:
  print('True')

>>> True

Does that make sense?

The expressions are broken up and evaluated like this:

Screenshot 2024-05-24 223558

Ah, thank you, that makes sense. So I know that I can create two if statements to fix this or an elif, but is there a way to make it one if statement to condense it?

Actually nvm, I just realized

1 Like

Yes, just make the first expression more like the second expression

1 Like

Welcome to the forum, keep on crushing it!

Tell us what’s happening:

Hi all, thank you to those who helped me last night, I was able to fix all of my problems and got the code to work and run smoothly. Although I’m having an issue where despite the code producing the desired results it wont’t satisfy any of the test cases. I’ll try to include a ss to show the issue, but does anyone know if there is a way around this or will I have to change my entire code to make it how the test cases want it

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    line1 = ''
    line2 = ''
    line3 = ''
    line4 = ''
    if len(problems) > 5:
        print('Error: Too many problems')
    else:
        for problem in problems:
            split = problem.split(" ")
            num1 = split[0]
            operator = split[1]
            num2 = split[2]
            if operator == '*' or operator == '/':
                print("Error: Operator must be '+' or '-'.")
                break
            if not num1.isdigit() or not num2.isdigit():
                print('Error: Numbers must only contain digits.')
                break
            if len(num1) > 4 or len(num2) > 4:
                print('Error: Numbers cannot be more than four digits')
                break
            maximum = max(len(num1), len(num2))
            upperbound = maximum + 2
            lowerbound = len(num2) + 1
            line1 += (' ' * (upperbound - len(num1))) + num1 + '    '
            line2 += operator + (' ' * (upperbound - len(num2) - 1)) + num2 + '    '
            line3 += ('-' * upperbound) + '    '
            if operator == '+':
                answer = int(num1) + int(num2)
            if operator == '-':
                answer = int(num1) - int(num2)
            answer_str = str(answer)
            line4 += ((' ' * (upperbound - len(answer_str))) + answer_str) + '    '
    print(line1)
    print(line2)
    print(line3)
    if show_answers == True:
        print(line4)


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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project


Sorry here’s a better one that also shows the test case

Your function should not print anything, it should return the answers. To see the results you would have a line like:

print(arithmetic_arranger(["3801 - 2", "123 + 49"]))
1 Like

Alright thank you, I tried turning all of my print(line#) statements into return statements, but it only prints line1 and then when I comment it out, it prints line2. I was thinking of trying to fix this using something like \n, but I don’t think it can be applied to a return statement, is there another way to fix that?

You can only return once, that exits the function. You’ll need to add your line strings together into one output and return that.