Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

Help idk if I am on the right track or not. I am not sure how to continue. I am sorry if this question is too basic, I just started learning how to code this week. Thanks in advance!

Your code so far

def arithmetic_arranger(problems, show_answers=False):

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

    for problem in problems:
        prob = problems.split()

        if prob[1] == '*' or prob[1] == '/':
            return "Error: Operator must be '+' or '-'."

        if prob[0].isdigit() or prob[2].isdigit() == False:
            return"Error: Numbers must only contain digits."

        if (prob[0]) or len(prob[2]) > 4:
            return "Error: Numbers cannot be more than four digits."
    else: 
        if prob[1] == '+':
            answer = prob[0] + prob[2]
        else:
            if len(prob[2]) > len(prob[0]):
                answer = prob[2] - prob[0]
            else:
                answer = prob[0] - prob[2]
                  



Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

if it’s a subtraction, you can’t change the order of the operands

In Python, when you’re using the or keyword, you need to make sure you’re writing a full condition on both sides of the or.

if prob[0].isdigit() or prob[2].isdigit() == False:

This might not work the way you expect, because only the second part is a full condition. Python reads it like this:

if prob[0].isdigit() or  (prob[2].isdigit() == False)

So it checks if the first part is True, and then checks if the second part is False, but it doesn’t check if both parts are False. The correct version of using “== False” would be:

if prob[0].isdigit() == Flase or prob[2].isdigit() == False:

Since we already know that the “isdigit” function returns a boolean we don’t even need to check if it is False, we can just use the “not” keyword in Python:

if not prob[0].isdigit() or not prob[2].isdigit() :

This way, you’re clearly checking if either input is not made of digits, and it’s easier to read. In the next line:

if (prob[0]) or len(prob[2]) > 4:

There’s a small mistake. You’re trying to check the length of both strings, but you only called the “length” function one of the numbers. Currently the statement “if (prob[0])” just checks if the string is empty. Adding the correct logic and this function call in you get:

if len(prob[0]) > 4 or len(prob[2]) > 4: