Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
I can’t understand the error message

Your code so far
import re

def arithmetic_arranger(problems, solve=False):

if(len(problems) > 5):
return(“Error: Too many problems”)

first = “”
second = “”
lines = “”
operator = “”
string = “”

for item in problems:
if(re.search(“[^\s0-9.±]”, item)):
if re.search(r"/|*", item):
return “Error, operator must be ‘+’ or ‘-’.”
return “Only digits are accepted.”

firstnum = item.split()[0]
operator = str(item.split()[1])
secondnum = item.split()[2]

if len(firstnum) > 5 or len(secondnum) > 5:
  return("maaaximum length is 4 numbers please.")

sum = ""

if operator == "+":
  sum = str(int(firstnum) + int(secondnum))
elif operator == "-":
  sum = str(int(firstnum) - int(secondnum))

length = max(len(firstnum), len(secondnum)) + 2
top = str(firstnum).rjust(length)
bottom = operator + str(secondnum).rjust(length - 1)
line = ""
res = sum.rjust(length)
for s in range(length):
  line += '-'

sumx = ""

if item != problems[-1]:
  first += top + '    ' 
  second += bottom + '    '
  lines += line + '    '
  sumx += res

if solve:
  string = first + "\n" + second + "\n" + lines + "\n" + sumx
else: 
  string = first + "\n" + second + "\n" + lines
return string

Your browser information:

Opera or whatever it says below here

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

Challenge: Scientific Computing with Python Projects - Arithmetic Formatter

Link to the challenge:

My replit -

Let’s look at the first error: test_template[test_two_problems_arrangement1]

'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]'

expected_output = '  3801      123\n-    2    +  49\n------    -----'
3801    \n-    2    \n------
3801      123\n-    2    +  49\n------    -----

Looks like your output is missing the second problem “123 + 49”

Your output from the test in main.py also only shows the first problem.

Try running this code and make the changes as you go.

import re

def arithmetic_arranger(problems, solve=False):
if len(problems) > 5:
return “Error: Too many problems”

arranged_problems = []

for problem in problems:
    match = re.match(r"(\d+)\s*([+\-])\s*(\d+)", problem)
    
    if not match:
        return f"Error: Invalid problem: {problem}"
    
    first_num, operator, second_num = match.groups()
    
    if len(first_num) > 4 or len(second_num) > 4:
        return "Error: Numbers cannot be more than four digits."
    
    if operator not in "+-":
        return "Error: Operator must be '+' or '-'."
    
    length = max(len(first_num), len(second_num)) + 2
    top = first_num.rjust(length)
    bottom = operator + second_num.rjust(length - 1)
    line = '-' * length
    
    arranged_problems.append((top, bottom, line))

if solve:
    arranged_problems = [f"{top}\n{bottom}\n{line}" for top, bottom, line in arranged_problems]
else:
    arranged_problems = [f"{top}\n{bottom}\n{line.split('-')[0]}" for top, bottom, line in arranged_problems]

return "    ".join(arranged_problems)

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