告诉我们发生了什么:
在此详细描述你的问题。
请帮我看看是什么问题
在我自己的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 与科学计算项目 - 算术格式化
挑战的链接: