I’ve written a code that seemingly returns the desired outcome but it only manages to pass the error tests (the ones where the operator is wrong, or there aren’t only digits, etc) although as far as I know the output should be what is demanded by the test.
This is the project’s replit link: Devfile for my arithmetic formatter thing - Replit
Your code so far
def addingprotocol(comps):
try:
comps0int = int(comps[0])
comps2int = int(comps[2])
oplist.append(str(comps[1].strip()))
result = comps0int + comps2int
if len(comps[0].strip()) > len(comps[2].strip()) :
longestvar = len(comps[0].strip())
elif len(comps[0].strip()) < len(comps[2].strip()):
longestvar = len(comps[2].strip())
else:
longestvar = len(comps[0].strip())
tmpdashbarlength = longestvar + 2
dashbarlengths.append(tmpdashbarlength)
spclen1 = tmpdashbarlength - len(str(comps[0]))
var1f = spacebar[0:spclen1] + str(comps[0])
var1list.append(var1f)
spclen2 = tmpdashbarlength - len(str(comps[2]))
var2f = comps[1].strip() + spacebar[0:(spclen2-1)] + str(comps[2])
var2list.append(var2f)
spclen3 = tmpdashbarlength - len(str(result))
res3f = spacebar[0:(spclen3)] + str(result)
resultlist.append(res3f)
dashbarlists.append(dashbar[0:tmpdashbarlength])
except:
return("Error: Numbers must only contain digits.")
def substractprotocol(comps):
try:
comps0int = int(comps[0])
comps2int = int(comps[2])
oplist.append(str(comps[1].strip()))
result = comps0int - comps2int
if len(comps[0].strip()) > len(comps[2].strip()) :
longestvar = len(comps[0].strip())
elif len(comps[0].strip()) < len(comps[2].strip()):
longestvar = len(comps[2].strip())
else:
longestvar = len(comps[0].strip())
tmpdashbarlength = longestvar + 2
dashbarlengths.append(tmpdashbarlength)
spclen1 = tmpdashbarlength - len(str(comps[0]))
var1f = spacebar[0:spclen1] + str(comps[0])
var1list.append(var1f)
spclen2 = tmpdashbarlength - len(str(comps[2]))
var2f = comps[1].strip() + spacebar[0:(spclen2-1)] + str(comps[2])
var2list.append(var2f)
spclen3 = tmpdashbarlength - len(str(result))
res3f = spacebar[0:(spclen3)] + str(result)
resultlist.append(res3f)
dashbarlists.append(dashbar[0:tmpdashbarlength])
except:
return("Error: Numbers must only contain digits.")
resultlist = []
var1list = []
var2list = []
oplist = []
errorstatus = 0
dashbarlengths = []
dashbar = "------"
dashbarlists = []
spacebar = " "
space4bar = " "
def finalconcatenationT():
line1 = " "
line2 = " "
line3 = " "
line4 = " "
for number in range(len(var1list)) :
if number < len(var1list):
line1 = line1 + var1list[number] + space4bar
line2 = line2 + var2list[number] + space4bar
line3 = line3 + dashbarlists[number] + space4bar
line4 = line4 + resultlist[number] + space4bar
else:
line1 = line1 + var1list[number]
line2 = line2 + var2list[number]
line3 = line3 + dashbarlists[number]
line4 = line4 + resultlist[number]
global finalline
finalline = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4
return finalline
def finalconcatenationF():
line1 = " "
line2 = " "
line3 = " "
line4 = " "
for number in range(len(var1list)) :
if number < len(var1list):
line1 = line1 + var1list[number] + space4bar
line2 = line2 + var2list[number] + space4bar
line3 = line3 + dashbarlists[number] + space4bar
line4 = line4 + resultlist[number] + space4bar
else:
line1 = line1 + var1list[number]
line2 = line2 + var2list[number]
line3 = line3 + dashbarlists[number]
line4 = line4 + resultlist[number]
global finalline
finalline = line1 + "\n" + line2 + "\n" + line3
return finalline
def arithmetic_arranger(problems, truecond=None):
global errorstatus
finalline = ""
for problem in problems:
if len(problems) > 5:
return("Error: Too many problems.")
errorstatus = 1
break
comps = problem.split()
if len(comps[0]) < 5 and len(comps[2]) < 5 :
if comps[1] == "+":
addingprotocol(comps)
elif comps[1] == "-":
substractprotocol(comps)
else:
return("Error: Operator must be '+' or '-'.")
errorstatus = errorstatus + 1
else:
return("Error: Numbers cannot be more than four digits.")
errorstatus = 1
try:
test1 = int(comps[0])
test2 = int(comps[2])
except:
return("Error: Numbers must only contain digits.")
if errorstatus == 0 and truecond == True:
finalconcatenationT()
elif errorstatus == 0 and truecond != True:
finalconcatenationF()
else:
return("Looks like this blew up. Woops!")
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: