I keep getting a traceback and cant figure what I am doing wrong.
python3 main.py
Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter/main.py”, line 4, in
from arithmetic_arranger import arithmetic_arranger
File “/home/runner/boilerplate-arithmetic-formatter/arithmetic_arranger.py”, line 40
return(arranged_problems)
^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: ‘return’ outside function
exit status 1
Your code so far
def arithmetic_arranger(problems, display_answers=True):
if len(problems) > 5:
return(“Error: Too many problems.”)
first_line = “”
second_line = “”
dash_line = “”
answer_line = “”
for problem in problems:
parts = problem.split()
operand1, operator, operand2 = parts
if not (operand1.isdigit() and operand2.isdigit()):
return("Error: Each operand must be a positive integer")
if operator not in "+-":
return("Error: Operator must be '+' or '-'")
if len(operand1) > 4 or len(operand2) > 4:
return("Error: Numbers cannot be more than four digits")
width = max(len(operand1), len(operand2)) + 2
first_line += operand1.rjust(width) + " "
second_line += operator + operand2.rjust(width - 1) + " "
dash_line += "-" * width + " "
if display_answers:
if operator == "+":
answer = str(int(operand1) + int(operand2))
else:
answer = str(int(operand1) - int(operand2))
answer_line += answer.rjust(width) + " "
arranged_problems = first_line.rstrip() + “\n” + second_line.rstrip() + “\n” + dash_line.rstrip()
if display_answers:
arranged_problems += “\n” + answer_line.rstrip()
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/115.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: