Tell us what’s happening:
Strings come out looking Identical, have run through websites to try to figure out the differences. the tests are not passing and I have no clue why. Any help would be appreaciated.
Your code so far
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
return "Error: Too many problems."
string1 = ""
string2 = ""
string3 = ""
string4 = ""
for problem in problems:
problemComonents = problem.split()
if len(problemComonents[0]) > 4 or len(problemComonents[2]) > 4:
return "Error: Numbers cannot be more than four digits."
if problemComonents[1] != "+" and problemComonents[1] != "-":
print(problemComonents)
return "Error: Operator must be '+' or '-'."
try:
int(problemComonents[2])
int(problemComonents[0])
except:
return "Error: Numbers must only contain digits."
lengthOfSum = ""
if len(problemComonents[0]) > len(problemComonents[2]):
longer = (
problemComonents[1]
+ " "
+ (" " * (len(problemComonents[0]) - len(problemComonents[2])))
+ problemComonents[2]
+ " "
)
lengthOfSum = ("-" * (len(longer) - 4)) + " "
string1 += (
(" " * ((len(longer) - 4) - len(problemComonents[0])))
+ problemComonents[0]
+ " "
)
string2 += longer
string3 += lengthOfSum
else:
longer = (
(" " * (len(problemComonents[2]) - len(problemComonents[0]) + 2))
+ problemComonents[0]
+ " "
)
lengthOfSum = ("-" * (len(longer) - 4)) + " "
string1 += longer
string2 += problemComonents[1] + " " + problemComonents[2] + " "
string3 += lengthOfSum
if problemComonents[1] == "+":
string4 += (
(" " * ((len(lengthOfSum) - 4)
- len(str(int(problemComonents[0]) + int(problemComonents[2])))
)) + str(int(problemComonents[0]) + int(problemComonents[2])) + " "
)
else:
string4 += (
(
(" " * ((len(lengthOfSum) - 4)
- len(str(int(problemComonents[0]) - int(problemComonents[2])))
))
+ str(int(problemComonents[0]) - int(problemComonents[2]))
+ " "
))
if(show_answers == True):
return string1 + '\n' + string2 + '\n' + string3 + '\n' + string4
else:
return string1 + '\n' + string2 + '\n' + string3
print(arithmetic_arranger(["3801 - 2", "123 + 49"]))
print(" 3801 123\n- 2 + 49\n------ -----")
print(str(arithmetic_arranger(["3801 - 2", "123 + 49"])) == " 3801 123\n- 2 + 49\n------ -----")
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project