Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

Tell us what’s happening:

It keeps telling me nothing to repeat at position 0
raise source.error(“nothing to repeat”,
re.error: nothing to repeat at position 0

and my code doesnt pass some of the tests even though it should???

Your code so far

import re


def arithmetic_arranger(problems, show_answers=False):
    # Lines
    first_line = ""
    second_line = ""
    third_line = ""
    answer_line = ""
    
    
    for problem in problems:
        

        # Error check:
        if len(problems) > 5:
            return "Error: Too many problems."
        if not (re.match('+', problem)) or (re.match('-', problem)):
            return "Error: sign must be + or -"   
        if (re.search(r'[\W]', problem)):
            return "Error: Numbers must only contain digits."
        if len(first_operand) > 4 or len(second_operand) > 4:
            return "Error: Numbers cannot be longer than four digits."
        
        # Operands
        first_operand = problem.split()[0]
        sign = problem.split()[1]
        second_operand = problem.split()[2]
        line_seperator = "-"
        answer = ""

        
        if sign == "+":
            answer = int(first_operand) + int(second_operand)
            answer = str(answer)
        elif sign == "-":
            answer = int(first_operand) - int(second_operand)
            answer = str(answer)
        
        spacing = max(len(first_operand), len(second_operand))
        line1 = first_operand.rjust(spacing)
        line2 = sign + second_operand.rjust(spacing -1)
        num_of_line_seperator = int(spacing) 
        line = line_seperator.rjust(num_of_line_seperator, "-")
        result = answer.rjust(spacing)
    
        
        if problem != problems[-1]:
            first_line += line1 + "    "
            second_line += line2 + "    "
            third_line += line + "    "
            answer += result + "    "
        else:
            first_line += line1
            second_line += line2
            third_line += line
            answer += result

    if show_answers is False:
        equation = first_line + "\n" + second_line + "\n" + third_line
        return equation
    elif show_answers is True:
        equation = first_line + "\n" +second_line + "\n" + third_line + "\n" + answer_line
        return equation

print(arithmetic_arranger(["12+1"]))

Your browser information:

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

Challenge Information:

Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project

the + is a special character, you can’t use it literally without escaping it

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