Having a bit of trouble with this first project. It’s my first experience with Repl.it, so I could be simply doing something wrong.
I coded the program in Sublime, and had it working well enough to pass all of the tests that I put it through on there, and then pasted it into Repl.it. I had to repair a bunch of formating errors before the code would run.
Then it would throw test errors for all kinds of stuff, and I would change things around until it worked. After a number of hours, I still fail 4 of the 6 tests, so I would like some guidance on what I might be doing wrong.
Cheers
def arithmetic_arranger(problems, showResult=False):
#Check if there are more than 5 problems
if len(problems) > 5:
return ('Error: Too many problems.')
#Split problems into list_of_questions
list_of_questions = []
for item in problems:
#Check that the operator is - or +
list_of_questions.append(item.split())
#Check the numbers do no have more than 4 digits
for item in list_of_questions:
#Make empty lists that will hold the final data
line1 = []
line2 = []
line3 = []
line4 = []
for item in list_of_questions:
#Remove nondigits
if not int(len(item[0])) <= 4 or not int(len(item[2])) <= 4:
print('Error: Numbers cannot be more than four digits')
return ('Error: Numbers cannot be more than four digits')
if not item[0].isnumeric() or not item[2].isnumeric():
print("Error: Numbers must only contain digits.")
return("Error: Numbers must only contain digits.")
#Check that the operator is valid
if item[1] == '+' or item[1] == '-':
pass
else:
print('Error: Operator must be '+' or '-'.')
return ('Error: Operator must be '+' or '-'.')
#Find the answer
if item[1] == "+":
answer = int(item[0]) + int(item[2])
else:
answer = int(item[0]) - int(item[2])
#Define which of the numbers is longest, and format accordingly.
if len(item[0]) >= len(item[2]):
longest = int(len(item[0]))
shortest = int(len(item[2]))
line2.append(item[1] + (' ' * (longest + 1 - shortest) + item[2] + ' '))
line1.append(' ' * (longest + 2 - longest) + item[0] + ' ')
line3.append('-' * (longest + 2) + ' ')
if showResult == True:
line4.append('{0: >{1}}{2}'.format(answer, (longest + 2), ' '))
else:
longest = int(len(item[2]))
shortest = int(len(item[0]))
line2.append(item[1] + (' ' * (longest + 1 - longest) + item[2] + ' '))
line1.append(' ' * (longest + 2 - shortest) + item[0] + ' ')
line3.append('-' * (longest + 2) + ' ')
if showResult == True:
line4.append('{0: >{1}}{2}'.format(answer, (longest + 2), ' '))
if showResult:
arranged_problems = str(line1) + '\n' + str(line2) + '\n' + str(line3) + '\n' + str(line4) + '\n'
else:
arranged_problems = str(line1) + '\n' + str(line2) + '\n' + str(line3) + '\n'
return(arranged_problems)