Tell us what’s happening:
I have already finished the code but only the error part of the test case is right even the out of the code is accurate
Your code so far
def arithmetic_arranger(problems, show_answers=True):
operator=[]
operand=[]
upper_nums=[]
bottom_nums=[]
final_answer=[]
result=[]
str_upper=''
str_bottom=''
str_dash=' '
str_result=''
if len(problems)>4:
return 'Error: Too many problems.'
for exp in problems:
element=exp.split()
operator.append(element[1])
operand.append(element[0])
operand.append(element[2])
for sign in operator:
if sign in ['/','*']:
return "Error: Operator must be '+' or '-'."
for nums in operand:
if not nums.isdigit():
return 'Error: Numbers must only contain digits.'
if len(nums)>4:
return 'Error: Numbers cannot be more than four digits.'
for i in range(0,len(operand),2):
upper_nums.append(operand[i])
for i in range(1,len(operand),2):
bottom_nums.append(operand[i])
for i in range(len(upper_nums)):
if len(upper_nums[i]) > len(bottom_nums[i]):
str_upper+=''.join(' '+upper_nums[i])
str_bottom+=''.join(''+ ' ' + operator[i] +' '+' '*(len(upper_nums[i])-len(bottom_nums[i])) + bottom_nums[i])
str_dash+=' '+(len(upper_nums[i])+2)*'-'
elif len(upper_nums[i]) < len(bottom_nums[i]):
str_bottom+=''.join(' '+ operator[i]+' '+bottom_nums[i])
str_upper+=''.join(' '+' '*(len(bottom_nums[i])-len(upper_nums[i])) + upper_nums[i])
str_dash+=' '+(len(bottom_nums[i])+2)*'-'
else:
str_bottom+=''.join(' '+operator[i]+' '+bottom_nums[i])
str_upper+=''.join(' '+ upper_nums[i])
str_dash+=' '+(len(upper_nums[i])+2)*'-'
str_bottom+=' '
str_upper+=' '
str_dash+=' '
for i in range(len(operator)) :
if operator[i]=='-' :
result.append(str(int(upper_nums[i])-int(bottom_nums[i])))
else:
result.append(str(int(upper_nums[i])+int(bottom_nums[i])))
for j in range(len(result)):
if len(bottom_nums[j])<len(upper_nums[j]) and len(result[j])<len(upper_nums[j]):
str_result+= (len(upper_nums[j])-len(rsult[j])+2)*' '+''.join(result[j])
elif len(upper_nums[j])<len(bottom_nums[j]) and len(result[j])<len(bottom_nums[j]):
str_result+= (len(bottom_nums[j])-len(rsult[j])+2)*' '+''.join(result[j])
else:
str_result+= 3*' '+''.join(result[j])
str_result+=' '
str_upper+='\n'
str_bottom+='\n'
str_dash+='\n'
str_result+='\n'
final_answer = str_upper + str_bottom + str_dash + str_result
if show_answers:
return final_answer
print(f'\n{arithmetic_arranger(["3801 - 2", "123 + 49"])}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project