请问Build an Arithmetic Formatter Project为什么test都是×

请问我在pycharm中测试都是对的,为什么在课程中全是叉。。。谢谢

If you open the browser console you will see a more detaild output of the tests.

At a glance, your function is returning None.

If you need more help please post your code not a screenshot.

def arithmetic_arranger(problems, show_answers=False):
    problems_str = ' '.join(problems)

    problems_test = ' + '.join(problems).split()[::2]
    for i in range(len(problems_test)):
        if problems_test[i].isnumeric() == False:
            return(print('Error: Numbers must only contain digits.'))

    numbers= ''.join(char if char.isdigit() else ' ' for char in problems_str).split()
    numbers_up = numbers[::2]
    numbers_down = numbers[1::2]

    for i in range(len(numbers)):
        if len(numbers[i]) > 4:
            return(print('Error: Numbers cannot be more than four digits.'))

    sig = ''
    num_re = []
    num_re_int = []
    offset = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    line = []

    for char in problems_str:
        if char == '+':
            sig += char
        elif char == '-':
            sig += char
        elif char == '*':
            return(print("Error: Operator must be '+' or '-'."))
        elif char == '/':
            return(print("Error: Operator must be '+' or '-'."))

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

    else:
        for i in range(len(sig)):
            if sig[i] == '+':
                num_re_int.append(int(numbers_up[i]) + int(numbers_down[i]))
            elif sig[i] == '-':
                num_re_int.append(int(numbers_up[i]) - int(numbers_down[i]))

        num_re = list(map(lambda x:str(x), num_re_int))

        for i in range(len(sig)):
            dif = len(numbers_up[i])-len(numbers_down[i])
            if dif > 0:
                offset[i+5] = dif
            elif dif < 0:
                offset[i] = abs(dif)
            elif dif == 0:
                offset[i] = 0

        for i in range(len(sig)):
            j = 0
            bl = ''
            while j < offset[i]:
                bl = ' '+bl
                j += 1
            numbers_up[i] = bl + numbers_up[i]

        for i in range(len(sig)):
            j = 0
            bl = ''
            while j < offset[i+5]:
                bl = ' ' + bl
                j += 1
            numbers_down[i] = bl + numbers_down[i]

        for i in range(len(sig)):
            numbers_down[i] = sig[i] + ' ' + numbers_down[i]

        for i in range(len(sig)):
            linestr = ''
            for j in range(len(numbers_down[i])):
                linestr += '-'
            line.append(linestr)

        for i in range(len(line)):
            diff = len(line[i]) - len(num_re[i])
            j = 0
            while j < diff:
                num_re[i] = ' ' + num_re[i]
                j += 1

        if show_answers == True:
            return (print('  ' + '      '.join(numbers_up) + '\n' + '    '.join(numbers_down) + '\n' +'    '.join(line) + '\n'+'    '.join(num_re)))
        elif show_answers == False:
            return (print('  ' + '      '.join(numbers_up) + '\n' + '    '.join(numbers_down) + '\n' + '    '.join(line)))

code attached above
thanks a lot!
:slight_smile:

your function is still returning None, did you try to do any debugging?

i tried it in pycharm and i worked…
thank you!

I’m sorry but I have to say that your function still returns None

check with print("output", arithmetic_arranger(["21 + 34"])
so that you will see the output printed next to the word “output”


.
thanks!
i run it and it turned out to be like … it run the function first and print “output None”
i have no idea why it is not in sequence

i trimmed the code to meet the certain test
it seemed that the correct answer was returned in the console area, but there was still cross in the test area…

thanks!

check your return statements, what is your function returning?

THANK YOU!
Should be just return the result instead of return print!