Tell us what’s happening:
I cant seem to get the problems side by side, ive look at many articles and I still cant see what I am doing wrong. I know the code is correct, besides the fact that the problems do go side by side
Your code so far
def arithmetic_arranger(Mproblems, solver= False):
fnum_list = ''
operators_list = ''
snum_list = ''
lines = ''
solution = ''
# Limiting the problem to just 5
if len(Mproblems) > 5:
print('Error: Too many problems.')
#Loop
for problems in Mproblems:
problems = problems.split()
fnum_list = problems[0]
operators_list = problems[1]
snum_list = problems[2]
#Check if its a digit & Check the length of number is not greater than 5
if fnum_list.isdigit() and snum_list.isdigit():
if len(fnum_list) > 4 or len(snum_list) > 4:
print('Error: Numbers cannot be more than four digits.')
#Checking + or - only
if operators_list == '+':
solution = int(fnum_list) + int(snum_list)
elif operators_list == '-':
solution = int(fnum_list) - int(snum_list)
else:
print('Error: Operator must be + or - ')
#Defining the distance
distance = max(len(fnum_list) , len(snum_list)) + 2
#Structure of Math problems
topnum = str(fnum_list.rjust(distance))
botnum = str(operators_list.ljust(0)) + ' ' + str(snum_list.rjust(distance - 2))
solution_line = str(solution)
lines = '-' * distance
#Chekcing solver
if solver:
solution = topnum + "\n" + botnum + "\n" + lines + "\n" + solution_lin
return solution
else:
solution = topnum + "\n" + botnum + "\n" + lines
return solution
print(solution)
arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
Challenge: Arithmetic Formatter
Link to the challenge: