Tell us what’s happening:
I do not find the difference between my result and the asked result. Can someone help me?
Your code so far
def arithmetic_arranger(problems, show_answers=False):
string_first=[]
string_operand=[]
string_second=[]
fourth_line=[]
solution=[]
list_of_length=[]
first_line=""
second_line=""
string_divisor=""
error=""
length=int()
result=int()
#ERRORCheck if more then 5 problems
if len(problems) > 5:
error='Error: Too many problems.'
return error
#Split the strings into their elements
[string_first.append((x.split()[0])) for x in problems]
[string_operand.append((x.split()[1])) for x in problems]
[string_second.append((x.split()[2])) for x in problems]
#Loop over all problems
for x in range(len(problems)):
#ERRORCheck if there are only digits
for char in string_first:
if char.isdigit()==True:
continue
else:
error="Error: Numbers must only contain digits."
return error
for char in string_second:
if char.isdigit()==True:
continue
else:
error="Error: Numbers must only contain digits."
return error
#ERRORCheck if more then 4 digits
if int(len(string_first[x]))>4:
error="Error: Numbers cannot be more than four digits."
return error
if int(len(string_second[x]))>4:
error="Error: Numbers cannot be more than four digits."
return error
#Search for the longest of the two operands
else:
if int(len(string_first[x]))>=int(len(string_second[x])):
length=int(len(string_first[x]))
elif int(len(string_first[x]))<int(len(string_second[x])):
length=int(len(string_second[x]))
list_of_length.append(length)
#Definition of the first and the second line of the calculation
for x in range(len(problems)):
if x < len(problems):
first_line+=string_first[x].rjust(list_of_length[x]+2)+" "
second_line+=string_operand[x]+" "+string_second[x].rjust(list_of_length[x])+" "
else:
first_line+=string_first[x].rjust(list_of_length[x]+2)
second_line+=string_operand[x]+" "+string_second[x].rjust(list_of_length[x])
#Definition of the dashes
string_divisor=""
for x in range(list_of_length[x]+2):
string_divisor+="-"
string_divisor+=" "
fourth_line.append(string_divisor)
#Calculation of the result of each problem
for x in range(len(problems)):
if string_operand[x]=="+":
result=int(string_first[x])+int(string_second[x])
elif string_operand[x]=="-":
result=int(string_first[x])-int(string_second[x])
else:
error="Error: Operator must be '+' or '-'."
return error
solution.append(str(result).rjust(length+2))
#Converting each line into a string
first="".join(first_line)
second="".join(second_line)
third="".join(solution)
fourth="".join(fourth_line)
#Printing the the result
if not error:
answer=f"{first}\n{second}\n{fourth}"
#answer=first+"\\n"+second+"\\n"+fourth
if show_answers==True:
answer=f"{first}\n{second}\n{string_divisor}\n{third}"
#print(first+"\\n"+second+"\\n"+fourth)
return answer
Your browser information:
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:136.0) Gecko/20100101 Firefox/136.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project