Tell us what’s happening:
I’m doing the Arithmetic Formatter ex and I’ve done everything except the answer formatting. Now my program prints correctly but I don’t know how to put the prints next to each other like shown in the example output. Thought about using lists and join method to get the formatting correctly but couldn’t find a way to do it. Any advice on how to do it correctly? Thanks!
Your code so far
def arithmetic_arranger(problems, print_or_no=False):
arranged_problems = {}
error = True
if len(problems) <= 5:
for i in problems:
problem = i.split()
try:
number1 = int(problem[0])
number2 = int(problem[2])
if len(str(number1)) > 4 or len(str(number1)) > 4:
print(“Error: Numbers cannot be more than four digits.”)
elif problem[1] == “+”:
arranged_problems[" “.join(problem)] = number1+number2
elif problem[1] == “-”:
arranged_problems[” ".join(problem)] = number1-number2
else:
error = False
print(“Error: Operator must be ‘+’ or ‘-’.”)
except:
error = False
print(“Error: Numbers must only contain digits.”)
else:
print(“Error: Too many problems”)
if print_or_no and error:
maksimi = 0
lista = []
for calc, answer in arranged_problems.items():
parts = calc.split()
if len(parts[0]) > len(parts[2]):
maksimi = len(parts[0])
elif len(parts[0]) < len(parts[2]):
maksimi = len(parts[2])
else:
maksimi = len(parts[0])
print(f"{parts[0]:>{2+maksimi}}\n{parts[1]:2}{parts[2]:>{maksimi}}\n{'-'*(maksimi+2)}\n{answer:>{maksimi+2}}\n")
elif print_or_no == False and error:
maksimi = 0
lista = []
for calc, answer in arranged_problems.items():
parts = calc.split()
if len(parts[0]) > len(parts[2]):
maksimi = len(parts[0])
elif len(parts[0]) < len(parts[2]):
maksimi = len(parts[2])
else:
maksimi = len(parts[0])
print(f"{parts[0]:>{2+maksimi}}\n{parts[1]:2}{parts[2]:>{maksimi}}\n{'-'*(maksimi+2)}")
arithmetic_arranger([“32 + 8”, “1 - 3801”, “9999 + 9999”, “523 - 49”], True)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: