I am having issues with the string formatting. I’m not able to figure out how to do that. I trying the python string padding but there is still something which I’m not catching that will solve my problem.
Here is my code:
def arithmetic_arranger(problems, display_result= False):
if len(problems) > 5:
return "Error: Too many problems."
first_operand = []
operator = []
second_operand = []
result = []
for eq in problems:
eq_splitted = eq.split()
if not eq_splitted[0].isnumeric():
return "Error: Numbers must only contain digits."
if eq_splitted[1] not in ["+", "-"]:
return "Error: Operator must be '+' or '-'."
if len(eq_splitted[2]) > 4 or len(eq_splitted[2]) > 4:
return "Error: Numbers cannot be more than four digits."
first_operand.append(eq_splitted[0])
operator.append(eq_splitted[1])
second_operand.append(eq_splitted[2])
result.append(eval(eq))
# string formatting and arrangements in the expected format.
if display_result:
first_line = ""
second_line = ""
third_line = ""
fourth_line = ""
for i in range(len(first_operand)):
first_line += "{:>4}".format(first_operand[i])+" "*4
second_line += "{}{:>3}".format(operator[i], second_operand[i])+" "*4
third_line += "{}".format("-"*5)+" "*4
fourth_line += "{:>4}".format(result[i])+" "*4
arrng = first_line+"\n"+second_line+"\n"+third_line+"\n"+fourth_line
return arrng
else:
first_line = ""
second_line = ""
third_line = ""
fourth_line = "" # result of the operation of every equation!
for i in range(len(first_operand)):
first_line += "{:>4}".format(first_operand[i])+" "*4
second_line += "{}{:>3}".format(operator[i], second_operand[i])+" "*4
third_line += "{}".format("-"*5)+" "*4
arrng = first_line+"\n"+second_line+"\n"+third_line
return arrng
#print(arranged_problems)
print(arithmetic_arranger(["32 + 6", "1 - 2", "45 + 43", "123 + 2249"], True))
print("\n")
print(arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"]))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36 Edg/91.0.864.37
So there are too many spaces at the end of the lines, which is easily solved by stripping them off.
A bit harder are the lengths of the other lines (all of them, operands, result and dash line), they need to be dependent on whichever is the longest.
Can you add that to your code and see what is the result?
This is really the hardest part of this challenge, so you are almost there!
That looks much better
Did you see the 1 Error and 2 Fail messages on replit?
It works on your machine because you test only numeric operands, try to replace the 2nd operand of the numbers with a letter and it will stop (this is what the Error tells you).
For the Fails: you solved the alignment, now the extra spaces at the end of the lines are still there. You’re done once you strip those off.
@Brain150 , I actually have solved the previous Issue before your response arrived and now I ran into another one. Could you please again help to point out what’s wrong.
def arithmetic_arranger(problems, display_result= False):
if len(problems) > 5:
return "Error: Too many problems."
first_operand = []
operator = []
second_operand = []
result = []
for eq in problems: #["98 + 3g5", "3801 - 2", "45 + 43", "123 + 49"]
eq_splitted = eq.split()
if not (eq_splitted[0].isnumeric() and eq_splitted[2].isnumeric()):
return "Error: Numbers must only contain digits."
if eq_splitted[1] not in ["+", "-"]:
return "Error: Operator must be '+' or '-'."
if len(eq_splitted[2]) > 4 or len(eq_splitted[2]) > 4:
return "Error: Numbers cannot be more than four digits."
first_operand.append(eq_splitted[0])
operator.append(eq_splitted[1])
second_operand.append(eq_splitted[2])
result.append(eval(eq))
# string formatting and arrangements in the expected format.
if display_result:
first_line = ""
second_line = ""
third_line = ""
fourth_line = ""
for i in range(len(first_operand)):
first_line += first_operand[i].rjust(max(len(first_operand[i]), len(second_operand[i]))+2)+" "*4
second_line += operator[i]+" "+second_operand[i].rjust(max(len(first_operand[i]), len(second_operand[i])))+" "*4
third_line += "-" * (max(len(first_operand[i]), len(second_operand[i]))+2)+" "*4
fourth_line += str(result[i]).rjust(max(len(first_operand[i]), len(second_operand[i]))+2)+" "*4
arranged_problems = first_line+"\n"+second_line+"\n"+third_line+"\n"+fourth_line
return arranged_problems
else:
first_line = ""
second_line = ""
third_line = ""
fourth_line = "" # result of the operation of every equation!
# for i in range(len(first_operand)):
# first_line += "{:>4}".format(first_operand[i])+" "*4
# second_line += "{}{:>3}".format(operator[i], second_operand[i])+" "*4
# third_line += "{}".format("-"*5)+" "*4
#
# arrng = first_line+"\n"+second_line+"\n"+third_line
for i in range(len(first_operand)):
#print(first_operand[i], second_operand[i])
first_line += first_operand[i].rjust(max(len(first_operand[i]), len(second_operand[i]))+2)+" "*4
second_line += operator[i]+" "+second_operand[i].rjust(max(len(first_operand[i]), len(second_operand[i])))+" "*4
third_line += "-" * (max(len(first_operand[i]), len(second_operand[i]))+2)+" "*4
arranged_problems = first_line+"\n"+second_line+"\n"+third_line
return arranged_problems
the error now which I am getting is from the test.py file.