Hello Everyone! Sharing with you all my code for this project. Please feel free to criticize.
def arithmetic_arranger(problems, show_answers=False):
items = []
operators = []
first_row = []
second_row = []
width = []
answers = []
answersString = ""
if len(problems)>5:
return'Error: Too many problems.'
else:
#Provides separation of each items in the problem
for problem in problems:
tempList = problem.split(" ")
items.append(tempList)
#Distributes the items and width of each problem
for item in items:
if item[0].isdigit() and item[2].isdigit():
if len(item[0])>4 or len(item[2])>4:
return 'Error: Numbers cannot be more than four digits.'
else:
first_row.append(item[0])
second_row.append(item[2])
width.append(max(len(item[0]),len(item[2]))+2)
else:
return 'Error: Numbers must only contain digits.'
if item[1]=="/" or item[1]=="*":
return "Error: Operator must be '+' or '-'."
else:
operators.append(item[1])
#Solves the problems and displays if set to True
if show_answers:
answersString = "\n"
for i in range(len(problems)):
answersString += str(eval(problems[i])).rjust(int(width[i]))
if (i<len(width)-1):
answersString += " "
#prints first row
counter = 0
result = ""
for number in first_row:
result += number.rjust(width[counter])
if counter<len(operators)-1:
result = result + " "
counter +=1
#prints second row
result2 = ""
for i in range(len(operators)):
result2 += operators[i] + second_row[i].rjust(width[i]-1)
if i<len(operators)-1:
result2 = result2 + " "
#prints third row
dashes = ""
for i in range(len(width)):
for j in range(int(width[i])):
dashes += "-"
if i<len(width)-1:
dashes = dashes + " "
if show_answers: #if show answer is equal to True
return result + "\n" + result2 + "\n" + dashes + answersString
else:
return result + "\n" + result2 + "\n" + dashes