Tell us what’s happening:
I am having trouble getting the formatting for the problem to work. I can get the answers to print and each individual problem formats, but I am having trouble getting each answer on the same line. Any advice is appreciated, thank you!
Your code so far:
def arithmetic_arranger(problems, show_answers=True):
arranged_problems = []
if len(problems) > 5:
print("Error: Too many problems.")
for problem in problems:
problem = problem.split(" ")
op1 = problem[0]
operand = problem[1]
op2 = problem[2]
if operand == "+":
answer = int(op1) + int(op2)
elif operand == "-":
answer = int(op1) - int(op2)
if operand not in ['+', '-']:
print("Error: Operator must be '+' or '-'.")
break
elif op1.isdecimal() is False or op2.isdecimal() is False:
print("Error: Numbers must only contain digits.")
break
elif len(op1) > 4 or len(op2) > 4:
print("Error: Numbers cannot be more than four digits.")
break
else:
op1 = op1.rjust(5)
op2 = op2.rjust(4)
answer = str(answer).rjust(5)
if show_answers is True:
output = op1 + '\n' + operand + op2 + '\n' + '-----' + '\n' + str(answer).rjust(5) + '\n'
elif show_answers is False:
output = op1 + '\n' + operand + op2 + '\n' + '-----' + '\n'
arranged_problems.append(output)
return arranged_problems
print(arithmetic_arranger(["33 + 55", "2 - 99", "24 + 765", "4 - 8", "9956 + 1"], show_answers=True))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36
.
Challenge: Arithmetic Formatter
Link to the challenge: