# Arithmetic-formatter

Hi everyone. I’m pretty new with python and I’m having trouble with the exit format of the arithmetic problems (as many do). I compared the format with the tests, they look exactly the same, yet I cannot pass the tests =(
Let me share the code of the function (It is also in a link at the end of this message). Please I would appreciate if someone could tell me why I’m getting wrong results. Thanks!

``````def arithmetic_arranger(problems, show_result=False):
if len(problems)>5:
print('Error: Too many problems.')
quit()

first=[]
sign= []
second= []
spaces=[] #List that saves the maximum spaces for each operation
diff_spaces=[] #list used to accomodate lines 1 and 2 in the exit
result=[ ]

counter=0

for elem in problems:

x=elem.split()

# Convert the elements to int to make math operations
try:
first_int= int(x)
sec_int= int(x)
except:
print('Error: Numbers must only contain digits.')
quit()

if len(x)>4 or len(x)>4:
print('Error: Numbers cannot be more than four digits.')
quit()

#The first and second elements as well as the sign go in different lists
first.append(x)
sign.append(x)
second.append(x)

#Tracking the operand with the higher length, to order the output
if len(x)>=len(x):
length= len(x)
else:
length= len(x)

spaces.append(length)

difference= len(x)-len(x)
diff_spaces.append(difference)

#Math operations and checking for error in the sign

if sign[counter]=='+':
result.append(str(first_int+sec_int))
elif sign[counter]=='-':
result.append(str(first_int-sec_int))
else:
print('Error: Operator must be ''+'' or ''-'' ')
quit()

counter=counter+1
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36`

Challenge: Arithmetic Formatter

I’m really wondering where people got the idea to use quit() - that’s not even covered in the course, is it? Yet it seems to quite often be used…

Anyway, for a start: you are supposed to `return` the results, including the error messages. Not `print()` them because the program has no idea what’s in the console. Also not calling `quit()`.

Once you do that, we can take a look at the error messages Hi Jagaya, thanks for your quick response. I suppose I use quit() because I’been trying for method to quit from python in the console and that one worked out. I don’t remember If it was discussed in the course, to be honest. What do you recommend me to use instead? I don’t really see a problem in using it but… I’m a newbie.

On the other hand, I’ve actually written the code with the return sentence. I also pasted the link but it seems not to be in my previous message. Let me try again:

boilerplate-arithmetic-formatter-4 - Replit

Hopefuly, the link is just above this sentence. I’ll share the code again, just in case (still with the forbidden quit() statments )

def arithmetic_arranger(problems, show_result=False):
if len(problems)>5:
print(‘Error: Too many problems.’)
quit()

first=
sign=
second=
spaces= #List that saves the maximum spaces for each operation
diff_spaces= #list used to accomodate lines 1 and 2 in the exit
result=

counter=0

for elem in problems:

``````  x=elem.split()

# Convert the elements to int to make math operations
try:
first_int= int(x)
sec_int= int(x)
except:
print('Error: Numbers must only contain digits.')
quit()

if len(x)>4 or len(x)>4:
print('Error: Numbers cannot be more than four digits.')
quit()

#The first and second elements as well as the sign go in different lists
first.append(x)
sign.append(x)
second.append(x)

#Tracking the operand with the higher length, to order the output
if len(x)>=len(x):
length= len(x)
else:
length= len(x)

spaces.append(length)

difference= len(x)-len(x)
diff_spaces.append(difference)

#Math operations and checking for error in the sign

if sign[counter]=='+':
result.append(str(first_int+sec_int))
elif sign[counter]=='-':
result.append(str(first_int-sec_int))
else:
print('Error: Operator must be ''+'' or ''-'' ')
quit()

counter=counter+1
``````

#Each line of the otput is a string

line1=’’
line2=’’
line3=’’
line4=’’

#Each line is constructed in several steps, depending in how many operations we have
C=0
for elem in result:
if diff_spaces[C]>=0:
line1=line1 + 2*’ ’ + abs(diff_spaces[C]) * ’ ’ + first[C] + 4 * ’ ’
line2=line2 + sign[C] + ’ ’ + second[C] + 4 * ’ ’

``````  else:
line1=line1 + 2*' ' + first[C]+ 4 * ' '
line2=line2 + sign[C] + ' ' + abs(diff_spaces[C]) * ' ' + second[C] + 4 * ' '

line3=line3 + '-'*(spaces[C]+2) + 4 * ' '
``````

#Result line
if int(result[C])>=0 and int(result[C])<10000:
line4=line4 + 2 * ’ ’ + result[C] + 4 * ’ ’
else: #Formato para numeros negativos o >10000
line4=line4 + ’ ’ + result[C] + 4 * ’ ’

``````  C=C+1

#Saving the output of the function
``````

if show_result == True:
arranged_problems= line1 + ‘\n’ + line2 + ‘\n’ + line3 +’\n’ + line4
else:
arranged_problems= line1 + ‘\n’ + line2 + ‘\n’ + line3 +’\n’

return arranged_problems

The point of quit() is to terminate a program. You shouldn’t use it in a function unless the function is meant to terminate the program.

Also you are still printing the error messages - those should be returned.

As for the arranged problems, you add a pointless linebreak if the result is not displayed - that’s an error.

``````- - 698    - 3801    + 43    +  49    +  40
+ - 698    - 3801    + 43    +  49    +  40
?                                          ++++
``````

And here you got excess spaces at the end.

Oh okay! very useful recommendations and concepts! thanks! I’ll work on that then. Have a great weekend

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.