Scientific Computing with Python Projects - Arithmetic Formatter

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:

the error message is saying the return keyword in outside your function and I do not see any indentation in your code at all.In Python, Indentation is crucial to identify individual blocks of code. Here is an example of a function with identation

def function( a):
    a  += 1
    return a

identation is the whitespace before the return
check out this for more:
Indentation in Python with Examples (freecodecamp.org)

Pasting your code into the forum may have broken the formatting

Can you link to your replit please?

This is the issue I am having now:  python3 main.py
Traceback (most recent call last):
File “/home/runner/boilerplate-arithmetic-formatter-1/main.py”, line 4, in
from arithmetic_arranger import arithmetic_arranger
File “/home/runner/boilerplate-arithmetic-formatter-1/arithmetic_arranger.py”, line 31, in
first_line += operand1.rjust(width) + " "
AttributeError: ‘tuple’ object has no attribute ‘rjust’

My replit

you set your operand1, operator, operand2 to be tuples then you tried to use a string method on them trun them into strings if you want to use the rjust method on them

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.