Python - Problem with the Arithmetic Formatter

Hello,
I am not getting the expected output. Line 10 and 39 of the test module.
I did a comparison between the output and the expected and the boolean were True.
Could you help please ?

import re
import numpy as np

def arithmetic_arranger(problems, var=None):
    if var == None or var:
        ret = ''
        ans = 0
        upper_num = []
        lower_num = []
        opperand = []
        answer = []
        dash = []
        upper_txt = ''
        lower_txt = ''
        dash_txt = ''
        answer_txt = ''
        if len(problems) > 5.1:
            return('Error: Too many problems.')
        else:        
            for i in problems:
                x = i.split(" ")
                try:
                    if len(x[0]) > 4:
                        raise ValueError
                    if len(x[2]) > 4:
                        raise ValueError
                except ValueError:
                    return("Error: Numbers cannot be more than four digits.")
                    break
                try:
                    x[0] = int(x[0])
                    x[2] = int(x[2])
                except ValueError:
                    return('Error: Numbers must only contain digits.')
                    break
                try:   
                    if str(x[1]) != '+' and str(x[1]) != '-':
                        raise Exception
                    if x[1] == '+': 
                        ans = int(x[0]) + int(x[2])
                    if x[1] == '-': 
                        ans = int(x[0]) - int(x[2])
                except Exception:
                    return("Error: Operator must be '+' or '-'.")
                    break
                a = str(x[0])
                upper_num = upper_num + [a]
                b = str(x[2])
                lower_num = lower_num + [b]
                c = str(x[1])
                opperand = opperand + [c]
                d = str(ans)
                answer = answer + [d]
        t = 0
        opperand = ["{}{}".format(i,' ') for i in opperand]
        while t < len(upper_num):
            max = np.maximum(len(upper_num[t]),len(lower_num[t])) + 2
            if not(t+1 == len(upper_num)):
                upper_txt = upper_txt + upper_num[t].rjust(max) + '    '
                lower_txt = lower_txt + opperand[t] + lower_num[t].rjust(max-2) + '    '
                dash_txt = dash_txt + ((max)*'-') + '    '
                answer_txt = answer_txt + answer[t].rjust(max) + '    '
            else:
                upper_txt = upper_txt + upper_num[t].rjust(max)
                lower_txt = lower_txt + opperand[t] + lower_num[t].rjust(max-2)
                dash_txt = dash_txt + ((max)*'-')
                answer_txt = answer_txt + answer[t].rjust(max)
            t += 1
        if len(answer) == len(problems):
                str.rstrip(upper_txt)
                str.rstrip(lower_txt)
                str.rstrip(dash_txt)
                if var :
                    print(upper_txt+'\n'+lower_txt+'\n'+dash_txt+'\n'+answer_txt)
                else:
                    print(upper_txt+'\n'+lower_txt+'\n'+dash_txt)

I don’t see your function returning anything when there are no errors

1 Like