No idea how to make it side by side nor add the space. Arithmetic formatter

I’ve been trying to code the arithmetic formatter, however i can’t seem to make the numbers appear side by side, so instead, i tried thinking about how i can add space between them but same result. I have no idea.

``````type or paste code here
def arithmetic_arranger(x, y=False):
for i in x:
if len(x) > 5:
print("Error too many problems")
break
a = i.split()
b = a[0]
c = a[1]
d = a[2]
if len(b) > 4 or len(d) > 4:
print("Error: Numbers cannot be more than four digits.")
break
if len(b) > len(c+d):
z = b.rjust(len(c+d)+1)
print(z)
print(c, d)
print("-" * len(b))
try:
if y == True:
e = int(b)
f = int(d)
if c == "+":
num = e+f
ab = str(num)
elif c == "-":
num = e-f
ab = str(num)
else:
print("Error: Operator must be '+' or '-")
except:
print("Error: Numbers must only contain digits")
break
elif len(b) <= len(c+d):
z = b.rjust(len(c+d)+1)
print(z)
print(c, d)
print("-" * (len(c+d)+1))
try:
if y == True:
e = int(b)
f = int(d)
if c == "+":
num = e+f
ab = str(num)
elif c == "-":
num = e-f
ab = str(num)
else:
print("Error operator must be + or -")
except:
print("Error: Numbers must only contain digits")
break

arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True)
``````

sorry, here’s the code

``````type or paste code here
def arithmetic_arranger(x, y=False):
for i in x:
if len(x) > 5:
print("Error too many problems")
break
a = i.split()
b = a[0]
c = a[1]
d = a[2]
if len(b) > 4 or len(d) > 4:
print("Error: Numbers cannot be more than four digits.")
break
if len(b) > len(c+d):
z = b.rjust(len(c+d)+1)
print(z)
print(c, d)
print("-" * len(b))
try:
if y == True:
e = int(b)
f = int(d)
if c == "+":
num = e+f
ab = str(num)
elif c == "-":
num = e-f
ab = str(num)
else:
print("Error: Operator must be '+' or '-")
except:
print("Error: Numbers must only contain digits")
break
elif len(b) <= len(c+d):
z = b.rjust(len(c+d)+1)
print(z)
print(c, d)
print("-" * (len(c+d)+1))
try:
if y == True:
e = int(b)
f = int(d)
if c == "+":
num = e+f
ab = str(num)
elif c == "-":
num = e-f
ab = str(num)
else:
print("Error operator must be + or -")
except:
print("Error: Numbers must only contain digits")
break

arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True)
``````

Simple: You don’t “`print()`” or at least not until you have aligned it in the first place.
Because `print()` gives the output to the console. Which first off doesn’t work because the tests don’t look at the console. And second off you cannot manipulate anything already in the console, as a console is “add lines” only.

So you have to make all calculations first, align stuff side-by-side. And only after that is all done, can you `return` the result (for this task) or `print()` if you want to.

that’s where i’m stuck, how can i change my code to align it side by side?

Most common strategy is to create 4 variables - one for each line. And then aling the content in there.

i have an idea but not sure if it works.
make a list, and get the whole operation with the dashes…, into one item in the list, then create a for loop with return result, end=“” to align the items in that list.
Can this work?

`return`” ends the function, so putting it into a loop won’t do much. Also it doesn’t take any arguments, so this “`end=""`” doesn’t do anything.

The tests expect a single `string` to be returned (refer to the `test_module.py`), so you gotta make that `string`.

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