创建算术格式化器项目 - Build an Arithmetic Formatter Project

告诉我们发生了什么:

I think my output is the same as in test, at least from the console, but I can’t pass anything except the error test.

到目前为止你的代码

def arithmetic_arranger(problems, show_answers=False):

    
    #judgment len of list max 5
    if len(problems) > 5:
        return 'Error: Too many problems.'
        
    else:
        fn_list = []
        sn_list = []
        ans_list = []
        mins_list = []
        is_last_iteration = False
        for index, problem in enumerate(problems):
            # judgment len of number max 4
            first_number = problem[:problem.index(' ')]
            second_number = problem[problem.index(' ')+3:]
            len_of_fn = len(first_number)
            len_of_sn = len(second_number)
            if len_of_fn and len_of_sn > 4:
                return 'Error: Numbers cannot be more than four digits.'
                # break
            # judgment if all number
            try:
                first_number = int(first_number)
                second_number = int(second_number)
            # print(type(first_number))
            except ValueError:
                return 'Error: Numbers must only contain digits.'
                
            # Judgment operator            
            operator = problem[problem.index(' ') + 1]
            if operator not in ('+','-'):
                return "Error: Operator must be '+' or '-'."       
                
            # print(type(operator))
            # print(operator)
            else:
                # calculate answer
                if operator in '+':
                    answer = first_number + second_number  
                else:
                    answer = first_number - second_number
                # start transfer        
                                    
                # the position of operator after transfer
                operator_position_len = max(len_of_fn, len_of_sn) + 2
                # print(operator_position)
                    
                
                # transfer to vertical
                fn_str = str(first_number)
                sn_str = str(second_number)
                if index == len(problems) - 1:
                    is_last_iteration = True

                if is_last_iteration:
                    fn_list.append(' ' * (operator_position_len - len_of_fn) + fn_str)
                    if len_of_fn <= len_of_sn:
                        sn_list.append(operator + ' ' + sn_str + ' ' * 4)
                    else:
                        sn_list.append(operator + ' ' * (operator_position_len - len_of_sn - 1) + sn_str)
                    mins_list.append('-' * operator_position_len)
                    ans_str = str(answer)
                    ans_list.append(' ' * (operator_position_len - len(ans_str)) + ans_str + ' ' * 4)
                    break
                fn_list.append(' ' * (operator_position_len - len_of_fn) + fn_str + ' ' * 4)
                # print(fn_list)
                if len_of_fn <= len_of_sn:
                    sn_list.append(operator + ' ' + sn_str + ' ' * 4)
                else:
                    sn_list.append(operator + ' ' * (operator_position_len - len_of_sn - 1) + sn_str + ' ' * 4)
                # print(sn_list)                  
                    
                mins_list.append('-' * operator_position_len + ' ' * 4)
                # print(mins_list)

                ans_str = str(answer)
                ans_list.append(' ' * (operator_position_len - len(ans_str)) + ans_str + ' ' * 4)
                # print(ans_list)
        
                
        
        fn_list.append('\n')
        sn_list.append('\n')
        if not show_answers:            
            problems = [''.join(s for i in [fn_list, sn_list, mins_list] for s in i)]
            
        else:
            mins_list.append('\n')
            problems = [''.join(s for i in [fn_list, sn_list, mins_list, ans_list] for s in i)]
        # print(problems)
        problems = problems[0].strip(" ")
        
        
        

    return repr(problems).strip("'")
print(f'{arithmetic_arranger(["3801 - 2", "123 + 49"])}')

你的浏览器信息:

用户代理是: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36

挑战信息:

创建算术格式化器项目 - Build an Arithmetic Formatter Project

Press f12 and check the devtools console for a more detailed error.