Python 与科学计算项目 - 算术格式化

告诉我们发生了什么:
在此详细描述你的问题。
请帮我看看是什么问题
在我自己的vscode上运行没问题,上到replit就报错
File “/home/runner/boilerplate-arithmetic-formatter-4/arithmetic_arranger.py”, line 37, in arithmetic_arranger
b.append(l[1])
IndexError: list index out of range

你目前的代码

def arithmetic_problems(problems):
    l = problems
    t = True
    if len(l) > 5:
        print('Error: Too many problems.')
        t = False
    for i in l:
        str = i.split(' ')
        if str[1] != '+' and str[1] != '-':
            print('Error: Operator must be "+" or "-".')
            t = False
        try:
            int(str[0])
            int(str[2])
        except:
            print('Error: Numbers must only contain digits.')
            t = False
        if len(str[0]) > 4 or len(str[2]) > 4:
            print('Error: Numbers cannot be more than four digits.')
            t = False
    x = (l, t)
    return arithmetic_arranger(x)


def arithmetic_arranger(x):
    
    l = []
    a = []
    b = []
    c = []
    str1 = x[0]

    for i in str1:
        l = i.split(' ')
        a.append(l[0])       
        b.append(l[1])
        c.append(l[2])

    def m(a, c):
        return max(len(a), len(c))+2-len(a)

    def m1(a, c):
        return max(len(a), len(c))+1-len(c)
    for i in range(len(str1)):
        print(" "*2+(m(a[i], c[i])*' '+a[i]+' '*2), end='')
    print()
    for i in range(len(str1)):
        print(" "*2+b[i]+(m1(a[i], c[i])*' '+c[i]+' '*2), end='')
    print()
    for i in range(len(str1)):
        print(" "*2+((max(len(a[i]), len(c[i]))+2)*'-'+" "*2), end='')
    print()
    for i in range(len(str1)):
        e = str(int(a[i])+int(c[i]))
        d = max(len(a[i]), len(c[i]))+2
        f = d-len(e)
        print(" "*2+f*' '+e+" "*2, end='')


arithmetic_problems(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])

你的浏览器信息:

用户代理是: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0

挑战: Python 与科学计算项目 - 算术格式化

挑战的链接:

在线项目中入口函数是 main.py, 它其中已经定义了对函数 arithmetic_arranger 的调用, 题目愿意是让我自行实现 arithmetic_arranger 函数以实现对 main.py 中调用过程的兼容. 函数仅需要返回字符串(return arranged_problems), 而不需要打印(print).
你的程序本身没有问题, 它在本地可以运行. 只是恰好它不符合题目要求. 你需要改造它, 将 arithmetic_arranger 函数做为你的程序入口, 而非 arithmetic_problems. 并且你的程序不需要打印任何内容, 只需要将结果拼装到一个字符串里返回给 main.py, 它会打印.

1 Like

感谢你的帮助!
我自己检查了很多遍,以为是断言方面的问题,我再尝试一下。
算法题是最好玩的,让人上瘾,就和健身一样。
再次感谢你