Tell us what’s happening:
This is the Repl.it : https://repl.it/@PhotoCoog/boilerplate-arithmetic-formatter-1#arithmetic_arranger.py
From what I can tell, my output strings are formatted correctly. I put in some print functions there for each case so I can see how it actually looks and it looks right but when the test module looks at it, I think it’s seeing something else. I’ve been staring at this for 3 hours so my eyes and mind are shot so any help is appreciated.
Your code so far
def arithmetic_arranger(problems, optionalSolve=False):
listLength = len(problems)
#these vars will hold the temporary parsed oprands from each object
augend = ""
addend = ""
operation = ""
answer=0
lengthMaxOpperand=0
#these vars will be the strings that will print out once we are done
firstLine=""
secondLine=""
dashLine=""
answerLine=""
arrangedProblems=""
# check for maximum of 5 problems
if listLength > 5:
return ("Error: Too many problems.")
#parse the list
for i in problems:
#apparently split is a little different in Python than in Javascript
augend, operation, addend = i.split()
#check to see if input is number
if augend.isnumeric() == False or addend.isnumeric()== False:
return ("Error: Numbers must only contain digits.")
#check to make sure numbers aren't more than 4 digits
if int(augend)>9999 or int(addend)>9999:
return("Error: Numbers cannot be more than four digits.")
#check to ensure + or - and do operation
if operation =="+":
answer= int(augend)+int(addend)
elif operation =="-":
answer = int(augend)-int(addend)
else:
return ("Error: Operator must be '+' or '-'.")
#Now we build each line using rjust() and max()
#first we find out the length of the larger of the two numbers
lengthMaxOpperand = len(max(augend,addend))
firstLine = firstLine + augend.rjust(lengthMaxOpperand+2) + " "
secondLine = secondLine + operation + " " + addend.rjust(lengthMaxOpperand) + " "
dashLine = dashLine + '-'*(lengthMaxOpperand+2) + " "
answerLine = answerLine + str(answer).rjust(lengthMaxOpperand+2) + " "
#Before we splice everything together, we need to check to see if we need to solve or not
#leaving print in there for debugging
if optionalSolve == True:
arrangedProblems=firstLine + "\n" + secondLine + "\n" + dashLine + "\n" + answerLine
print (optionalSolve)
print(arrangedProblems)
else:
arrangedProblems=firstLine + "\n" + secondLine + "\n" + dashLine
print (optionalSolve)
print(arrangedProblems)
return (arrangedProblems)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36
.
Challenge: Arithmetic Formatter
Link to the challenge: