# Can't tell why this if statement throws error

I’m working on the arithmetic_arranger project and I have an if statement on line 31 of my code:

if i < len(problems):

that keeps throwing an invalid syntax error, and I can’t tell what’s wrong with it. Here’s my code:

``````def arithmetic_arranger(problems, solutions=False):
#check that problems isn't too long
if len(problems) > 5:
return 'Error: Too many problems.'

arranged_problems = ''

#split earch string into its 2 numbers and its operand
for i, v in enumerate(problems):
v = v.split()
#if operand is anything other than + or -, return error
if (v != '+' and v != '-'):
return 'Error: Operator must be \'+\' or \'-\'.'
#if any number is greater than 4 digits, return error
if (len(v) > 4 or len(v) > 4):
return 'Error: Numbers cannot be more than four digits.'
#if any number contains characters other than numbers, return error
if (~v.isdigit() or ~v.isdigit()):
return 'Error: Numbers must only contain digits.'

#for each math problem there's a width, equal to 2 + len(longest number)
if (len(v) > len(v)):
longest = len(v)
else:
longest = len(v)
width = longest + 2

#insert spaces before the top number
i.insert(0, ' ' * (width - len(i))
#insert spaces after the top number, except for the last problem
if i < len(problems):
v.insert(2, '   ')
else:
v.insert(2, '\n')
#insert spaces between operand and bottom number
v.insert(4, ' ' * (width - 1 - len())
#insert spaces after the bottom number, except for the last problem
if i < len(problems):
v.append('   ')
else:
v.append('\n')
#insert dashes
v.append('-' * width)
#insert space after dashes
if i < len(problems):
v.append('   ')
else:
v.append('\n')
#insert optional solutions
if solutions == True:
#calculate solution and turn it into a string
if v == '+':
v.append(str(int(v) + int(v)))
else:
v.append(str(int(v) - int(v)))
#insert space before solution
v.insert(10, ' ' * (width - len(v))
#insert space after solution
if i < len(problems):
v.append('   ')
else:
v.append('\n')

#populate line1
for i in problems:
arranged_problems = arranged_problems + i + i + i
#populate line2
for i in problems:
arranged_problems = arranged_problems + i + i + i + i
#populate line3
for i in problems:
arranged_problems = arranged_problems + i + i
#populate optional line4
if solutions == True:
for i in problems:
arranged_problems = arranged_problems + i + i + i

return arranged_problems
``````

Issue here is in line with code before that, take a look at brackets:

``````		v.insert(4, ' ' * (width - 1 - len())
``````
1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.