# I'm finding this waaay too hard

Tell us what’s happening:
I ran my code and it worked fine while I was testing locally, but when used the test module provided, my code didn’t pass a single test. I didn’t make the code with it giving more than two numbers per problem. Should I modify the code or start from scratch? but even if I did I have no other ideas on how to solve this…

``````def arithmetic_arranger(problems, Sum = None):
# Preparation...
ch_1 = list()
ch_2 = list()
ch_3 = list()
ch_4 = list()
ch_4_1 = list()
problem_space = list()
space_needed = list()
space_between = list()
ans_space = list()
dump = list()

# Too many problems detection code.
if len(problems) > 5:
return print("Error: Too many problems.")
quit()

# Separating into different lists.
for problem in problems:
nums = problem.split()
ch_1.append(nums)
ch_2.append(nums)
ch_3.append(nums)

# Wrong operator detection code.
for opr in ch_2:
# print(opr)
if opr == "+" or opr == "-":
continue
else:
return print("Error: Operator must be '+' or '-'.")
quit()

# Too many digits and wrong characters detection code.
for dig in ch_1:
# print(dig)
try:
dump.append(int(dig))
if len(dig) > 4 :
return print("Error: Numbers cannot be more than four digits.")
quit()
except:
return print("Error: Numbers must only contain digits.")
quit()

for dig in ch_3:
# print(dig)
try:
dump.append(int(dig))
if len(dig) > 4 :
return print("Error: Numbers cannot be more than four digits.")
quit()
except:
return print("Error: Numbers must only contain digits.")
quit()

# Calculating spaces code, and constructing sums list code.
for problem in problems:
nums = problem.split()
# print(nums)
problem_space.append(max(len(nums), len(nums)) + 2)
# print(len(nums), len(nums))
if nums == "+":
ch_4.append(int(nums) + int(nums))
elif nums == "-":
ch_4.append(int(nums) - int(nums))
if len(nums) >= len(nums):
space_needed.append(6)
space_between.append((abs(len(nums) - len(nums))) + 1)
elif len(nums) < len(nums):
space_needed.append((len(nums) - len(nums)) + 6)
space_between.append(1)
if nums == "+":
cur_sum = str(int(nums) + int(nums))
elif nums == "-":
cur_sum = str(int(nums) - int(nums))
ans_space.append((max(len(nums), len(nums)) + 2) - len(cur_sum))

# Accounting for first arithmetic problem spacing.
first = problems.split()
if len(first) >= len(first):
space_needed = 2
elif len(first) < len(first):
space_needed = (len(first) - len(first)) + 2

# Turning the sums list into strings.
for num in ch_4:
ch_4_1.append(str(num))

# Debugging..
# print(ch_1)
# print(ch_2)
# print(ch_3)
# print(ch_4)
# print(problem_space)
# print(space_needed)
# print(space_between)
# print(ans_space)

# Displaying the problems in a neat way.
if len(problems) == 1:
arranged_problems = " " * space_needed + ch_1 + "\n" + ch_2 + " " * space_between + ch_3 + "\n" + "-" * problem_space
if Sum == True:
arranged_problems = arranged_problems + "\n" + " " * ans_space + ch_4_1
return arranged_problems

if len(problems) == 2:
arranged_problems = " " * space_needed + ch_1 + " " * space_needed + ch_1 + "\n" + ch_2 + " " * space_between + ch_3 + " " * 4 + ch_2 + " " * space_between + ch_3 + "\n" + "-" * problem_space + " " * 4 + "-" * problem_space
if Sum == True:
arranged_problems = arranged_problems + "\n" + " " * ans_space + ch_4_1 + " " * 4 + " " * ans_space + ch_4_1
return arranged_problems

if len(problems) == 3:
arranged_problems = " " * space_needed + ch_1 + " " * space_needed + ch_1 + " " * space_needed + ch_1 + "\n" + ch_2 + " " * space_between + ch_3 + " " * 4 + ch_2 + " " * space_between + ch_3 + " " * 4 + ch_2 + " " * space_between + ch_3 + "\n" + "-" * problem_space + " " * 4 + "-" * problem_space + " " * 4 + "-" * problem_space
if Sum == True:
arranged_problems = arranged_problems + "\n" + " " * ans_space + ch_4_1 + " " * 4 + " " * ans_space + ch_4_1 + " " * 4 + " " * ans_space + ch_4_1
return arranged_problems

if len(problems) == 4:
arranged_problems = " " * space_needed + ch_1 + " " * space_needed + ch_1 + " " * space_needed + ch_1 + " " * space_needed + ch_1 + "\n" + ch_2 + " " * space_between + ch_3 + " " * 4 + ch_2 + " " * space_between + ch_3 + " " * 4 + ch_2 + " " * space_between + ch_3 + " " * 4 + ch_2 + " " * space_between + ch_3 + "\n" + "-" * problem_space + " " * 4 + "-" * problem_space + " " * 4 + "-" * problem_space + " " * 4 + "-" * problem_space
if Sum == True:
arranged_problems = arranged_problems + "\n" + " " * ans_space + ch_4_1 + " " * 4 + " " * ans_space + ch_4_1 + " " * 4 + " " * ans_space + ch_4_1 + " " * 4 + " " * ans_space + ch_4_1
return arranged_problems

if len(problems) == 5:
arranged_problems = " " * space_needed + ch_1 + " " * space_needed + ch_1 + " " * space_needed + ch_1 + " " * space_needed + ch_1 + " " * space_needed + ch_1 + "\n" + ch_2 + " " * space_between + ch_3 + " " * 4 + ch_2 + " " * space_between + ch_3 + " " * 4 + ch_2 + " " * space_between + ch_3 + " " * 4 + ch_2 + " " * space_between + ch_3 + " " * 4 + ch_2 + " " * space_between + ch_3 + "\n" + "-" * problem_space + " " * 4 + "-" * problem_space + " " * 4 + "-" * problem_space + " " * 4 + "-" * problem_space + " " * 4 + "-" * problem_space
if Sum == True:
arranged_problems = arranged_problems + "\n" + " " * ans_space + ch_4_1 + " " * 4 + " " * ans_space + ch_4_1 + " " * 4 + " " * ans_space + ch_4_1 + " " * 4 + " " * ans_space + ch_4_1 + " " * 4 + " " * ans_space + ch_4_1
return arranged_problems

arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True)
``````

User Agent is: `Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36`.

Challenge: Arithmetic Formatter

Don’t get discouraged because you failed all the tests. You are very close to the solution already, so keep going!

Your code looks good so I’d recommend reworking it instead of starting from scratch.

The reason you are not passing the tests is one basic thing: Look at what the tests expect you to `return`.
Currently you are return print-functions, which I find kinda funny because I didn’t know you could do that and it would just work. It’s still going to fail tests because this actually returns None - the print function merely puts out the string but it doesn’t create a return-value.
Also `return` exits the function already, you don’t need a following `quit()`.

So basically, remove the “print”-keyword in the returns and that might already bring you a lot closer to passing the tests.

I didn’t make the code with it giving more than two numbers per problem.

Yeah well read the assignment → because that’s exactly what you were supposed to do 1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

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