Hi,
I am working on the first task in scientific computing with python: arithmetic formatter
( Scientific Computing with Python Projects - Arithmetic Formatter | Learn | freeCodeCamp.org
I am wondering why my code is not working if I run it in Replit
I am just starting to code, so it would be great to hear about improvements
Cosi
def arithmetic_arranger(problems, result = False):
# divide the calculation
firstline = ""
secondline = ""
dashline = ""
resultline = ""
for problem in problems:
number1 = problem.split()[0]
operator = problem.split()[1]
number2 = problem.split()[2]
#print(number1)
#print(number2)
# check operator
if not (operator == "+" or operator == "-"):
return("Error: Operator must be '+' or '-'.")
# check if digit
if not (number1.isdigit or number2.isdigit):
return("Error: Numbers must only contain digits.")
# length of numbers
if (len(number1) > 4 or len(number2) > 4):
return("Error: Numbers cannot be more than four digits.")
# find longest_number
number_list = [number1, number2]
longest_word = max(number_list, key=len)
i = len(longest_word)
#print(i)
# alignment
upper_line = number1.rjust(i + 2)
bottom_line = operator + number2.rjust(i + 1)
#print(upper_line)
#print(bottom_line)
# if result = true
if result == True:
if operator == "+":
sum = str(int(number1) + int(number2))
else:
sum = str(int(number1) - int(number2))
sum = sum.rjust(i + 2)
dash = "-" * (i)
dash = dash.rjust(i + 2)
#print(dash)
#print(result)
#print("\n")
firstline = firstline + upper_line + " "
secondline = secondline + bottom_line + " "
dashline = dashline + dash + " "
resultline = resultline + sum + " "
print(firstline)
print(secondline)
print(dashline)
print(resultline)