Tell us what’s happening:
I’m stuck on the last two test questions, i dont know how to edit my code so it passes and i keep getting this error message
TypeError: arithmetic_arranger() takes 1 positional argument but 2 were given
Your code so far
def arithmetic_arranger(problems):
# Checks to see if number of arguments is more than 5
if len(problems) > 5:
return 'Error: Too many problems.'
# Format for arithmetic
arranged_sol = ''
arranged_problems = ''
top_line = ''
middle_line = ''
dash_line = ''
for problem in problems:
problem_list = problem.split()
# Checks to see if arguments are digits
if not problem_list[0].isdigit() or not problem_list[2].isdigit():
return 'Error: Numbers must only contain digits.'
# Ensures that the length of the digits is not greater than 5
if len(problem_list[0]) > 4 or len(problem_list[2]) > 4:
return 'Error: Numbers cannot be more than four digits.'
# Ensures that only plus and negative operators are used
if problem_list[1] not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
# Calculates the width of the the arrenger format
width = max(len(problem_list[0]), len(problem_list[2])) + 2
# Takes the first index in list, adjust to the right based on the width
top_line += problem_list[0].rjust(width) + ' '
# Inserts operator to the left and adds second index
middle_line += problem_list[1] + ' ' + problem_list[2].rjust(width - 2) + ' '
# Calculates the number of dashes to add based on width
dash_line += '-' * width + ' '
# Calculates solution based on operator used
if problem_list[1] == '+':
solution = int(problem_list[0]) + int(problem_list[2])
else:
solution = int(problem_list[0]) - int(problem_list[2])
# Adds the solution of the arguments below the dash line
arranged_sol += '\n' + ' ' * (width - len(str(solution))) + str(solution) + ' '
# Arranges the lines and dashes together
arranged_problems = top_line.rstrip() + '\n' + middle_line.rstrip() + '\n' + dash_line.rstrip() + arranged_problems
if True in problems:
return arranged_sol
else:
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/120.0.0.0 Safari/537.36 Edg/120.0.0.0
Challenge Information:
Scientific Computing with Python Projects - Arithmetic Formatter