Tell us what’s happening:
@ilenia this is probably the one millionth time someone is asking for this kind of help. but i’ve been stuck on trying to arrange the problems right but i keep getting errors, the link to my code is above, maybe you’ll see something i’m not, thank you for taking time to check it out
LINK: https://repl.it/@OyeyiolaMaryam/boilerplate-arithmetic-formatter-2#arithmetic_arranger.py
Your code so far
def arithmetic_arranger(problems, solution=False):
line1 = ''
line2 = ''
line3 = ''
line4 = ''
if len(problems) > 5:
return "Error: Too many problems."
for problem in problems:
pieces = problem.split()
num1 = pieces[0]
op = pieces[1]
num2 = pieces[2]
if not ((op == '+') or (op == '-')):
return "Error: Operator must be '+' or '-'."
if (len(num1) > 4) or (len(num2) > 4):
return "Error: Numbers cannot be more than four digits."
if not (num1.isnumeric()) or (num2.isnumeric()):
return "Error: Numbers must only contain digits."
#total = 0
if (op == '+'): solution = num1 + num2
elif (op == '-'): solution = num1 - num2
width = max(len(num1), len(num2)) + 2
space = ' '
line1 = str(num1).rjust((len(num2) - len(num1)) + 2) + space
line2 = op + str(num2).rjust((len(num1) - len(num2)) + 1) + space
line3 = str('-' * (width)) + space
line4 = str(solution).rjust(width) + space
if solution == True:
arranged_problems = line1 +'\n' + line2 + '\n' + line3 + '\n' + line4
else:
arranged_problems = line1 +'\n' + line2 + '\n' + line3
return arranged_problems
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36
.
Challenge: Arithmetic Formatter
Link to the challenge: