Although I still have some error nevertheless:
-“Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithmetic problems and a second argument of True.”
-“Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithmetic problems and a second argument of True.”
- “Expected calling “arithmetic_arranger()” with a problem that contains a letter character in the number to return “Error: Numbers must only contain digits.””
Here’s the updated code:
def arithmetic_arranger(problems, showResults = False):
numberProblems = len(problems)
if numberProblems < 5:
answer = ""
for i in range(len(problems)):
newOperation = problems[i].split()
try:
int(newOperation[0]) and int(newOperation[2])
if len(newOperation[0]) < 5 and len(newOperation[2]) < 5:
firstOperand = f"{newOperation[0]:>5}"
secondOperand = f"{newOperation[2]:>3}"
line = ""
if len(newOperation[0]) > len(newOperation[2]):
for y in range(len(newOperation[0]) + 2):
line = line + "-"
else:
for y in range(len(newOperation[2]) + 2):
line = line + "-"
if newOperation[1] == "+":
result = int(newOperation[0]) + int(newOperation[2])
elif newOperation[1] == "-":
result = int(newOperation[0]) - int(newOperation[2])
else:
return "Error: Operator must be '+' or '-'."
finalResult = f"{str(result):>5}"
answer = answer + firstOperand + "\n" + newOperation[1] + " " + secondOperand + "\n" + line + "\n" + finalResult + "\n" + "\n" + "\n" + "\n"
showResults = True
else:
return "Error: Numbers cannot be more than four digits."
except:
return "Error: Numbers must contain digits."
else:
return "Error: Too many problems."
if showResults:
return answer
I believe you may have misunderstood the instructions. The instructions had stated that there should be 4 spaces between each problem. Your code puts 4 newline characters instead. As for your final error, you just forgot to put the word “only.” For clarification. your error returns
“Error: Numbers must contain digits.”
rather than
“Error: Numbers must only contain digits.”
I hope that clears things up a bit.
All right, I changed once again my code. There was an improvement since now I have 2 erros instead of three
. Here’s my new code:
def arithmetic_arranger(problems, showResults = False):
numberProblems = len(problems)
if numberProblems < 5:
firstLine = ""
secondLine = ""
line = ""
finalResult = ""
for i in range(len(problems)):
newOperation = problems[i].split()
firstLine = firstLine + str(f"{newOperation[0]:>5}") + " "
secondLine = secondLine + str(newOperation[1]) + " " + str(f"{newOperation[2]:>3}") + " "
if len(newOperation[0]) > len(newOperation[2]):
for y in range(len(newOperation[0]) + 2):
line = line + "-"
else:
for y in range(len(newOperation[2]) + 2):
line = line + "-"
line = line + " "
try:
int(newOperation[0]) and int(newOperation[2])
if len(newOperation[0]) < 5 and len(newOperation[2]) < 5:
if newOperation[1] == "+":
result = int(newOperation[0]) + int(newOperation[2])
elif newOperation[1] == "-":
result = int(newOperation[0]) - int(newOperation[2])
else:
return "Error: Operator must be '+' or '-'."
finalResult = finalResult + str(f"{str(result):>5}") + " "
showResults = True
else:
return "Error: Numbers cannot be more than four digits."
except:
return "Numbers must only contain digits."
else:
return "Error: Too many problems."
if showResults:
solvedProblem = firstLine + "\n" + secondLine + "\n" + line + "\n" + finalResult
return solvedProblem
else:
unsolvedProblem = firstLine + "\n" + secondLine + "\n" + line
return unsolvedProblem
And this are my errors:
-“Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithmetic problems and a second argument of True.”
-Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”]
Something still seems to be wrong with your formatting. Your code outputs the following:
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
730 3799 88 172
In reality, it should look like this:
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
730 3799 88 172
Keep in mind that there should not be spaces at the end of each line, only between the problems.
I updated my “line” code (the “—”) in a simpler and nicer way but the problem persist:
def arithmetic_arranger(problems, showResults = False):
numberProblems = len(problems)
if numberProblems < 5:
firstLine = ""
secondLine = ""
line = ""
finalResult = ""
for i in range(len(problems)):
newOperation = problems[i].split()
firstLine = firstLine + str(f"{newOperation[0]:>5}") + " "
secondLine = secondLine + str(newOperation[1]) + " " + str(f"{newOperation[2]:>3}") + " "
**lineLength = max(len(newOperation[0]), len(newOperation[2])) + 2**
** for y in range(lineLength):**
** line = line + "-"**
** line = line + " "**
try:
int(newOperation[0]) and int(newOperation[2])
if len(newOperation[0]) < 5 and len(newOperation[2]) < 5:
if newOperation[1] == "+":
result = int(newOperation[0]) + int(newOperation[2])
elif newOperation[1] == "-":
result = int(newOperation[0]) - int(newOperation[2])
else:
return "Error: Operator must be '+' or '-'."
finalResult = finalResult + str(f"{str(result):>5}") + " "
showResults = True
else:
return "Error: Numbers cannot be more than four digits."
except:
return "Error: Numbers must only contain digits."
else:
return "Error: Too many problems."
if showResults:
solvedProblem = firstLine + "\n" + secondLine + "\n" + line + "\n" + finalResult
return solvedProblem
else:
unsolvedProblem = firstLine + "\n" + secondLine + "\n" + line
return unsolvedProblem
Regarding the thing of the space, I have no idea how to create spaces only between the problems withouth breaking my code.
Your code still has the same problems.
To get rid of the spaces at the end, you should use the .strip() method on each line. It will get rid of any whitespace (spaces, newlines, etc.) at the beginning and end of the string. It also takes an optional parameter if you want to get rid of a specific letter or string rather than whitespace, but I don’t think you need that here. Here is a link for more information on the method: Python string | strip() - GeeksforGeeks
Done. I added that and changed my code entirely. Looks good in my text editor (even fixed the previous problem of the lines) but I still got two errors.
Here’s my new code:
def arithmetic_arranger(problems, showResults = False):
firstLine = ""
secondLine = ""
lines = ""
finalResult = ""
if len(problems) < 5:
for i in range(len(problems)):
newOperation = problems[i].split()
firstNumber = str(newOperation[0])
operator = str(newOperation[1])
secondNumber = str(newOperation[2])
line = ""
lineLength = max(len(newOperation[0]), len(newOperation[2])) + 2
for y in range(lineLength):
line = line + "-"
try:
int(newOperation[0]) and int(newOperation[2])
if len(newOperation[0]) < 5 and len(newOperation[2]) < 5:
if newOperation[1] == "+":
result = int(newOperation[0]) + int(newOperation[2])
elif newOperation[1] == "-":
result = int(newOperation[0]) - int(newOperation[2])
else:
return "Error: Operator must be '+' or '-'."
top = firstNumber.rjust(lineLength)
bottom = operator + secondNumber.rjust(lineLength - 1)
res = str(result).rjust(lineLength)
else:
return "Error: Numbers cannot be more than four digits."
except:
return "Error: Numbers must only contain digits."
if i != problems[-1]:
firstLine += top + ' '
secondLine += bottom + ' '
lines += line + ' '
finalResult += res + ' '
else:
firstLine += top
secondLine += bottom
lines += line
finalResult += res
else:
return "Error: Too many problems."
firstLine.rstrip()
secondLine.rstrip()
lines.rstrip()
if showResults:
finalResult.rstrip()
arranged_problems = firstLine + "\n" + secondLine + "\n" + lines + "\n" + finalResult
else:
arranged_problems = firstLine + "\n" + secondLine + "\n" + lines
return arranged_problems
print(arithmetic_arranger(["32 + 698", "3815 - 2", "45 + 43", "123 + 49"], showResults = True))
And here are the errors:
-“Expected solutions to be correctly displayed in output when calling “arithmetic_arranger()” with arithmetic problems and a second argument of True.”
-“Expected different output when calling “arithmetic_arranger()” with [“3 + 855”, “3801 - 2”, “45 + 43”, “123 + 49”]”
.rstrip() doesn’t mutate (change) the string, it just returns the new “stripped” string. So rather than
firstLine.rstrip()
secondLine.rstrip()
...
you need
firstLine = firstLine.rstrip()
secondLine = secondLine.rstrip()
...
FINALLY! Thank you so muchhhhhh for the patience, it really means a lot. Have a great weekend friend 
Always glad to help! You too!
