How to print the arrangement in arithmetic formatter

Tell us what’s happening:

Hello fellow campers, I’m trying to complete the arithmetic formatter and what I’ve done so far is create a list of lists called “data” , where each list the information i want to display vertically side by side, I wanted to make a string I could return and print.

However I am not able to figure out how to make a loop to print out the lists or arrange them in a single string to be able to print the expected result, I would appreciate it your inputs.

Your code so far

def arithmetic_arranger(a,b):
count=0
count_x=0
l=len(a)
table_data=list()
output=list()
data=list()

#print(l)
#print(a)
if l>5:
    print("Error: Too many problems.")
    quit()
while count<l:
    #print(count)
    #print(a[count])
    pro=a[count]
    y=pro.split()
    count=count+1

    for words in y:
        check_num_len=len(words)
        #print(words)
        if check_num_len>4:
            print("Error: Numbers cannot be more than four digits.")
            quit()
        if words.isnumeric()==False:
            if words=="+" or words=="-":
                pass
            elif words=="*" or words=="/":
                print("Error: Operator must be '+' or '-'.")
                quit()
            else:
                print("Error: Numbers must only contain digits.")
                quit()

for item in a:
    table_data.append(item.split())

#print(table_data)

col_width = max(len(num) for item in table_data for num in item) + 1


for item in table_data:
    output.append(item[0].rjust(col_width+1))
    output.append(item[1] + item[2].rjust(col_width))
    output.append('-' * col_width)
    
    if b==True:
        if item[1]=="+":
            addition_op=str(int(item[0])+int(item[2]))
       
            output.append(addition_op.rjust(col_width+1))


        if item[1]=="-":
            addition_op=str(int(item[0])-int(item[2]))
            output.append(addition_op.rjust(col_width+1))

    #print(output)
    data.append(output)
    output=list()




res = "\n".join("{} {}".format(x, y) for x, y in zip(data[0], data[1],data[2],data[3],data[4]))

probs=list()
while True:
x=input("Insert 5 addition or subtraction problems ")
if x==“done” or x==“Done”:
break
else:
probs.append(x)
continue

cond=input("Do you want the problem to be solved? True or False ")
if cond==“True” or cond==“true”:
cond=True
elif cond==“False” or cond==“false”:
cond=False
else:
cond=False

arithmetic_arranger(probs,cond)

Your browser information:

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

Challenge: Arithmetic Formatter

Link to the challenge:

For easier testing, you should hardcode test-cases instead of using inputs. As it can get quite tedious to always write the same test-cases.

As for the arranger - you are supposed to “return” the resulting string, not print anything.

And as for how to do it… well first it would be interesting to see how your output looks now, to get a feeling for what your code does produce.
Additionally maybe some error messages as those are pretty helpful figuring out missing details if the solution generally looks correct but still fails.

Hello Jagaya, I hard coded the test case as you suggested (ty)

I am working with the following array which goes in the variable “data”:

Do you want the problem to be solved? True or False True
[[’ 32’, ‘+ 698’, ‘-----’, ’ 730’], [’ 3801’, ‘- 2’, ‘-----’, ’ 3799’], [’ 45’, ‘+ 43’, ‘-----’, ’ 88’], [’ 123’, ‘+ 49’, ‘-----’, ’ 172’]]

So my idea was that I had a list of lists , the list has the structure of operand, operand and operation, the line made of ---- and the answer, all of these are all ready given the spacing they need with the column with, so I wanted to make a string which combined these lists but I do not know how to make them vertical and then go back up , I tried doing it by adding new lines in a for loop, but no luck, so I’m kinda stuck on that, would really appreciate your help.

Best regards,

Juan Bedoya

Hello, I kind of figured it out, I added:

for x in data:
list_1.append(x[0])
list_2.append(x[1])
list_3.append(x[2])
if len(x)==4:
list_4.append(x[3])
# print(list_1)
# print(list_2)
# print(list_3)

# print(''.join(list_1))
# print(''.join(list_2))
# print(''.join(list_3))
# print(''.join(list_4))
c='    '.join(list_1) + "\n" + '    '.join(list_2) + "\n" + '    '.join(list_3) + "\n" + '    '.join(list_4) + "\n"
print(c)

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