Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

hello, me again

I’m trying to complete this project with no success.

i would like to run the first function ONLY if the second argument of the first function is set to true.

after hours non it with no success I’m turning to your for help.

any suggestion is appreciated.

thanks
regards
Andrea

Your code so far

def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5: #check if there are more than 5 problems
        return 'Error: too many problems'
    if any(op in problem for op in ['*','/'] for problem in problems): # division and multiplication error
        return "Error: Operator must be '+' or '-'."
    arranged_problems = []
    first_line = ""
    second_line = ""
    dashes_line = ""
    for problem in problems:
        num1, operator, num2 = problem.split()
        if not num1.isnumeric() or not num2.isnumeric():
            return "Error: Numbers must only contain digits."
        if len(num1) > 4 or len(num2) > 4:
            return "Error: Numbers cannot be more than four digits."
        # splitting the problem into num1, operator, num2
        parts = problem.split()
        num1 = parts[0]
        operator = parts[1]
        num2 = parts[2]
        # comparing the length of num1 and num2 pick the longest
        longest = max(len(num1), len(num2))
        # defining the length of the dashes line
        dashes = longest + 2
        # defining the 3 lines of the arranged problem
    problems = ["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]
    first_line = ""
    second_line = ""
    dashes_line = ""
    for problem in problems:
        # splitting the problem into num1, operator, num2
        parts = problem.split()
        num1 = parts[0]
        operator = parts[1]
        num2 = parts[2]
        # comparing the length of num1 and num2 pick the longest
        longest = max(len(num1), len(num2))
        dashes = longest + 2
        first_line += num1.rjust(dashes) + "    "
        second_line += operator + " " + num2.rjust(longest) + "    "
        dashes_line += "-" * (dashes + 1) + "    "
    result = first_line.rstrip() + "\n" + second_line.rstrip() + "\n" + dashes_line.rstrip()
    if show_answers:
        result += "\n" + " answers"
    return result

def show_only_if_true():
    if show_answers:
        print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], show_answers))

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

I’m not sure what you mean here. Could you rephrase it?

Hi
On the very first line of my code " def arithmetic_arranger(problems, show_answers=False)" it says show_answer=False.

I would like to re arrange the code to give the required result when that is set to true :slight_smile:

That is the parameter with default value. In case of calling the function with just one argument, the show_answers will have the False value.

Otherwise, when two arguments are passed during the function call, show_answers will have value as in the call.

To add to this:

You never call your show_only_if_true() function.

It looks like you want to call this function when arithmetic_arranger() is called with show_answers=True

Lastly, the show_answers variable is only accessible in the arithmetic_arranger() function, it’s local to that function. It’s not accessible from other functions.

hello sorry form the late reply.

Am i right in understanding that what i was trying to-do is not possible? or maybe i was doing the wrong way?

do i need to create an if statement within that function to make it work? or is just not possible?

which function?

you can’t access show_answers in show_only_if_true()

hello there
as for your first question: i was referring to the first function “def arithmetic_arranger(problems, show_answers=False):”

as for the second question
yes i understood that is not possible to access the value outside the first function.
i was wandering if i could do that on the first one or if jut not possible at all?

But isn’t that already in code?

def arithmetic_arranger(problems, show_answers=False):
    # (...)
    if show_answers:
        result += "\n" + " answers"
    return result

I seem to have another problem now the thing gave the result it want but still give errors

Code is

problems = ["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]
def arithmetic_arranger(problems, show_answers=False):
    if len(problems) > 5:
        # check if there are more than 5 problems
        return 'Error: Too many problems.'
    if any(op in problem for op in ['*', '/'] for problem in problems):
        # division and multiplication error
        return "Error: Operator must be '+' or '-'."
    for problem in problems:
        num1, operator, num2 = problem.split()
        if not num1.isnumeric() or not num2.isnumeric():  # operator should only contain number errors  message
            return 'Error: Numbers must only contain digits.'
        if len(num1) > 4 or len(num2) > 4:
            return 'Error: Numbers cannot be more than four digits.'
            # defining the 4 lines of the arranged problem
    first_line = ""
    second_line = ""
    dashes_line = ""
    result_line = ""
    for problem in problems:
        # splitting the problem into num1, operator, num2
        parts = problem.split()
        num1 = parts[0]
        operator = parts[1]
        num2 = parts[2]
        # comparing the length of num1 and num2 pick the longest
        longest = max(len(num1), len(num2))
        dashes = longest + 2  # defining dash line
        #  Formatting the 3 lines of the arranged problem
        first_line += num1.rjust(dashes) + "    "
        second_line += operator + " " + num2.rjust(longest) + "    "
        dashes_line += "-" * (dashes) + "    "
        result_line += str(eval(num1 + operator + num2)).rjust(dashes) + "    "
        result = first_line.rstrip() + "\n" + second_line.rstrip() + "\n" + dashes_line.rstrip()+ "\n" + result_line.rstrip()  # result line
    return result


arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True)
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')

Call your function using repr() like this:

print(repr(arithmetic_arranger([“32 + 8”, “1 - 3801”, “9999 + 9999”, “523 - 49”], True)))

and then compare it character by character to what the tests say it should return.

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 (').

1 Like

thanks mate ill try tomorrow at work now