You’ll run into at least two additional issues apart from what @JeremyLT said:
First you are not using the solve argument.
Second you will have excess spaces at the end of your lines.
Thanks @JeremyLT@Jagaya . Are you saying to remove the spaces completely at the end of “answer” and “dashes”? Also, I fixed the indentation level of “while solve:” so that it’s outside of the for loop. My code is still not passing the tests though. Still can’t figure out what’s wrong.
def arithmetic_arranger(problems, solve = False):
top = ""
bottom = ""
operator = ""
dashes = ""
answer = ""
first_line = ""
second_line = ""
if len(problems) >= 5:
return "Error: Too many problems."
for problem in problems:
top = problem.split(" ")[0]
if not top.isdigit():
return "Error: Numbers must only contain digits."
if int(top) > 9999:
return "Error: Numbers cannot be more than four digits."
bottom = problem.split(" ")[2]
if not bottom.isdigit():
return "Error: Numbers must only contain digits."
if int(bottom) > 9999:
return "Error: Numbers cannot be more than four digits."
operator = problem.split(" ")[1]
if operator != "+" or "-":
return "Error: Operator must be '+' or '-'."
for problem in problems:
top = problem.split(" ")[0]
operator = problem.split(" ")[1]
bottom = problem.split(" ")[2]
Max = max(len(top), len(bottom)) + 2
if problem != problems[-1]:
first_line += top.rjust(Max) + " "
second_line += operator + " " + bottom.rjust(Max - 2) + " "
dashes += "-" * Max + " "
else:
first_line += top.rjust(Max)
second_line += operator + " " + bottom.rjust(Max - 2) + " "
dashes += "-" * Max + " "
while solve:
if operator == "+":
answer += str(int(top) + int(bottom)).rjust(Max) + " "
return first_line, "\n", second_line, "\n", dashes, "\n", answer
else:
answer += str(int(top) - int(bottom)).rjust(Max) + " "
return first_line, "\n", second_line, "\n", dashes```
There is no link, please check again - it may fell victim to the forums auto-format.
Also as @JeremyLT already hinted at, you don’t concatenate strings with commas. Python interpets commas as part of a tuple. Strings get concatenated with “+”.
+ Error: Operator must be '+' or '-'.
- 3801 123
- - 2 + 49
- ------ -----
All errors look like this: You are returning this error message.
And the reason is quite simple
Your condition is: if operator != "+" or "-":
The “or” is executed AFTER the left and right statements are calculated. So you are checking if “operator != ‘+’” is true or “’-’” is true. And by definition a non-empty string is always True → hence this condition is always True → hence it always returns the error message.
Please look again on how to properly combine logical statements in code.