Hi,
I have been giving the arithmetic arranger challenge a go, but have run into an issue. The code I have made works when I test it in sublime, but when I try on repl it fails. I am struggling to understand what exactly is wrong. Could anyone give me a nudge in the right direction?
import re
def arithmetic_arranger(equations, answer):
if len(equations)>4:
print('Error: Too many problems')
return
'''setting up local variables'''
numbers=[]
allanswers=[]
operators=[]
dividingline='-----'''
'''looping through the equations'''
for e in equations:
'''checking if there are letters'''
c=re.search('[a-zA-Z]', e)
if c is not None:
print('Error: Numbers must only contain digits.')
return
'''pulling the numbers out'''
y=re.findall('[0-9]+', e)
'''checking if each set of numbers is 4 digits maximum'''
if len(y[0])>4:
print('Error: Numbers cannot be more than four digits.')
return
if len(y[1])>4:
print('Error: Numbers cannot be more than four digits.')
return
'''pulling the operator out'''
z=re.findall('[+-]', e)
'''checking if the operator is + or -, or if there are multiple operators'''
if len(z) <1:
print("Operator must be '+' or '-'")
return
if len(z) >1:
print('Expressions can only have one operator')
return
'''setting the calculation to + or -'''
if z[0]=='+':
operator=1
if z[0]=='-':
operator=2
'''performing the calculation'''
if operator==1:
equationanswer=int(y[0])+int(y[1])
stringanswer=str(equationanswer)
if operator==2:
equationanswer=int(y[0])-int(y[1])
stringanswer=str(equationanswer)
'''saving all the results'''
numbers.append(y[0])
numbers.append(y[1])
allanswers.append(stringanswer)
operators.append(z[0])
'''printing results'''
print(f'{numbers[0].rjust(6)} {numbers[2].rjust(6)} {numbers[4].rjust(6)} {numbers[6].rjust(6)}')
print(f'{operators[0]} {numbers[1].rjust(4)} {operators[1]} {numbers[3].rjust(5)} {operators[2]} {numbers[5].rjust(4)} {operators[3]} {numbers[7].rjust(4)}')
print(f'{dividingline.rjust(6)} {dividingline.rjust(6)} {dividingline.rjust(6)} {dividingline.rjust(6)}')
'''checking if answers should be shown, and printing'''
if answer==True:
print(f'{allanswers[0].rjust(6)} {allanswers[1].rjust(6)} {allanswers[2].rjust(6)} {allanswers[3].rjust(6)}')
arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True)