Hi, I am very new to coding and am struggling to understand my error messages in replit. The code works when I work on it when I haven’t defined it and am working on a single equation, but when I define it and put it onto replit the equation parts are not working and I’m not entirely sure why. Sorry if it’s simple, I am still new to this. Thanks in advance!
Here is the code:
def arithmetic_arranger(problems, solve = False):
if len(problems) > 5:
return "Error: Too many problems."
for problem in problems:
peices = problem.split()
# print(peices)
firstNumber = peices[0]
# check that firstNumber is only numbers
try:
int(firstNumber)
except:
return "Error: Numbers must only contain digits."
continue
#check that firstNumber is not more than four digits long
if len(firstNumber) > 4:
return "Error: Numbers cannot be more than four digits."
# print(firstNumber)
operator = peices[1]
# print(operator)
if not "+" or "-" in operator:
return "Error: Operator must be '+' or '-'."
secondNumber = peices[2]
# check that secondNumber is only numbers
try:
int(secondNumber)
except:
return "Error: Numbers must only contain digits."
continue
#check that secondNumber is not more than four digits long
if len(secondNumber) > 4:
return "Error: Numbers cannot be more than four digits."
# print(secondNumber)
# you need to find a way to change the print errors to return errors
# now doing the equation
firstLine = []
secondLine = []
thirdLine = []
fourthLine = []
for firstPosition in problems:
if len(firstNumber) > len(secondNumber):
firstLine.append(" "*2 + firstNumber)
else:
firstLine.append(" "*(len(secondNumber)-len(firstNumber) + 2)+ firstNumber)
# print(firstLine)
for secPosition in problems:
if len(firstNumber) > len(secondNumber):
secondLine.append(operator + " "*(len(firstNumber)-len(secondNumber) + 1) + secondNumber)
else:
secondLine.append(operator + " " + secondNumber)
# print(secondLine)
for dashes in problems:
if len(firstNumber) > len(secondNumber):
thirdLine.append("-"*(len(firstNumber) + 2))
else:
thirdLine.append("-"*(len(secondNumber) + 2))
# print(thirdLine)
for solution in problems:
if "+" in operator:
answer = int(firstNumber) + int(secondNumber)
else:
answer = int(firstNumber) - int(secondNumber)
answer = str(answer)
for ansPosition in problems:
if len(firstNumber) > len(secondNumber):
if len(firstNumber) > len(answer):
fourthLine.append(" "*(len(firstNumber)-len(answer)) + answer)
elif len(firstNumber) == len(answer):
fourthLine.append(" "*2 + answer)
else:
fourthLine.append(" "*(len(answer)-len(firstNumber)) + answer)
else:
if len(secondNumber) > len(answer):
fourthLine.append(" "*(len(secondNumber)-len(answer)) + answer)
elif len(secondNumber) == len(answer):
fourthLine.append(" "*2 + answer)
else:
fourthLine.append(" "*(len(answer)-len(secondNumber)) + answer)
# print(fourthLine)
arranged_problems = " ".join(firstLine) + "\n" + " ".join(secondLine) + "\n" + " ".join(thirdLine) + "\n" + " ".join(fourthLine)
print(arranged_problems)
return arranged_problems
please post a link to your replit by copying the URL within 2 angled brackets <URL>
Also you can copy the error messages from replit and post them here and we can help you understand them as well.
Edit: I just edited your post to make the replit link visible btw.
Try to edit your main.py to comment out the automated tests. This will allow you to slowly test your code until you believe it is in good shape and then run the automated tests when you are ready.
For eg. in main.py you have this right now
# This entrypoint file to be used in development. Start by reading README.md
from pytest import main
from arithmetic_arranger import arithmetic_arranger
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))
# Run unit tests automatically
main(['-vv'])
If you comment out the line main(['vv'])
and run, you will see this output:
python main.py
Error: Operator must be '+' or '-'.
So clearly this is the wrong output given that the input is print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))
So start by debugging this and let us know if you need help with this and or further debugging.
Thanks for the advice- commenting out the automated tests made it a lot easier to work on it.
I’ve now got it to a point where it is ALMOST working: it prints out all the equations as I split them, and it prints the final output in the way I wanted it formatted. The only problem is printing it in the final format only seems to loop through the last equation and I cannot figure out why!
def arithmetic_arranger(problems, solve = False):
if len(problems) > 5:
return "Error: Too many problems."
for problem in problems:
peices = problem.split()
print(peices)
firstNumber = peices[0]
operator = peices[1]
secondNumber = peices[2]
print(firstNumber)
print(operator)
print(secondNumber)
# check that firstNumber is only numbers
try:
int(firstNumber)
except:
return "Error: Numbers must only contain digits."
continue
#check that firstNumber is not more than four digits long
if len(firstNumber) > 4:
return "Error: Numbers cannot be more than four digits."
# print(firstNumber)
# print(operator)
if operator == "+"or "-":
continue
else:
return "Error: Operator must be '+' or '-'."
# check that secondNumber is only numbers
try:
int(secondNumber)
except:
return "Error: Numbers must only contain digits."
#check that secondNumber is not more than four digits long
if len(secondNumber) > 4:
return "Error: Numbers cannot be more than four digits."
# print(secondNumber)
# you need to find a way to change the print errors to return errors
# now doing the equation
#print(secondNumber)
firstLine = []
secondLine = []
thirdLine = []
fourthLine = []
for i in problems:
if len(firstNumber) > len(secondNumber):
firstLine.append(" "*2 + firstNumber)
else:
firstLine.append(" "*(len(secondNumber)-len(firstNumber) + 2)+ firstNumber)
for secPosition in problems:
if len(firstNumber) > len(secondNumber):
secondLine.append(operator + " "*(len(firstNumber)-len(secondNumber) + 1) + secondNumber)
else:
secondLine.append(operator + " " + secondNumber)
# print(secondLine)
for dashes in problems:
if len(firstNumber) > len(secondNumber):
thirdLine.append("-"*(len(firstNumber) + 2))
else:
thirdLine.append("-"*(len(secondNumber) + 2))
# print(thirdLine)
for solution in problems:
if "+" in operator:
answer = int(firstNumber) + int(secondNumber)
else:
answer = int(firstNumber) - int(secondNumber)
answer = str(answer)
for ansPosition in problems:
if len(firstNumber) > len(secondNumber):
if len(firstNumber) > len(answer):
fourthLine.append(" "*(len(firstNumber)-len(answer)) + answer)
elif len(firstNumber) == len(answer):
fourthLine.append(" "*2 + answer)
else:
fourthLine.append(" "*(len(answer)-len(firstNumber)) + answer)
else:
if len(secondNumber) > len(answer):
fourthLine.append(" "*(len(secondNumber)-len(answer)) + answer)
elif len(secondNumber) == len(answer):
fourthLine.append(" "*2 + answer)
else:
fourthLine.append(" "*(len(answer)-len(secondNumber)) + answer)
# print(fourthLine)
arranged_problems = " ".join(firstLine) + "\n" + " ".join(secondLine) + "\n" + " ".join(thirdLine) + "\n" + " ".join(fourthLine)
#print(arranged_problems)
return arranged_problems
hi there, just a quick comment, I noticed you are using print and actually you should not be doing that. You should be creating a string and returning it which when printed later displays the expected output.
the reason is that in the first for loop you wrote, you looped through the equations and the last thing you did was look at the last equation therefore firstNumber is the first number of the last equation.
Then later, after that first for loop ended, you started another for loop
and at this point firstNumber is set to the last first number you saw in the previous loop.
Hey there, thank you so much again for the help. I managed to fix my loops so that all the answers were put into the second loop and the output now seems to be exactly what they were looking for. Here is the output:
which seems to be exactly how they want it to be. However, when I remove the comments in main.py to allow the tests to run, it says 9/10 of the tests have failed. This is including the four tests (the ‘‘Error: Too many problems.’’, ‘‘Error: Operator must be ‘+’ or ‘-’.’’, ‘’ Error: Numbers must only contain digits.‘’ and ‘’ Error: Numbers cannot be more than four digits.‘’ ) which I had originally passed and dont think I made any changes to this part of the code. Not sure why it is failing when the output is what they want?
Thank you! This also helped me understand all the other error messages better and I figured out what they were asking for. I managed to fix 10/10 parameters and have now passed the arithmetic formatter challenge! Thank you so much for all your help!