Hey there, I’m relatively new to coding (<1year) and learned Python just recently. So last weekend I wrote the first Challenge (arithmetic formatter) in VSCode and everything works fine. However when I copy it into the Repl:.it it gives me a bunch of errors. I’ve tried to figure out how to fix it, but i think it’s fair to say that i’m officially stuck and not sure what what I’m doing wrong. Here is my code which works fine in my VSCode app.
def arithmetic_arranger(myList, arg = False):
firstList = []
secondList = []
thirdList = []
fourthList = []
if len(myList) > 5:
print("Error: Too many problems.")
else:
for each in myList:
eachNum = each.split(" ")
# print("Equation:", eachNum)
firstNum = eachNum[0]
secondNum = eachNum[2]
operator = eachNum[1]
# print("Operator:",operator)
#checking in operand are integer:
if firstNum.isdigit() == False:
print("Error: Numbers must only contain digits.")
quit()
elif secondNum.isdigit() == False:
print("Error: Numbers must only contain digits.")
quit()
numWidth = 0
if len(firstNum) + 2 >= len(secondNum) + 2:
numWidth = len(firstNum) + 2
elif len(firstNum) + 2 <= len(secondNum) + 2:
numWidth = len(secondNum) + 2
# print("Length of first element:", len(firstNum), "Length of complete firstLine:", len(firstNum) + 2)
# print("Length of second element:", len(secondNum), "Length of complete secondLine:", len(secondNum) + 2)
# print("Therefore numWidth is:", numWidth, "blocks long (spaces included)")
if numWidth > 6:
print("Error: Numbers cannot be more than four digits.")
quit()
#firstLine:
#spaceCount firstList
spaceFirstList = []
# print(numWidth - (len(firstNum) + 2), "spaces need to be created on the firstLine.")
for eachSpace in range(numWidth - (len(firstNum) + 2)):
spaceFirstList.append(" ")
#printing firstList
firstList.extend([" "])
firstList.extend(spaceFirstList)
firstList.extend([firstNum, " "])
#secondLine:
#spaceCount secondList
spaceSecondList = []
# print(numWidth - (len(secondNum) + 2), "spaces need to be created on the secondLine.")
for eachSpace in range(numWidth - (len(secondNum) + 2)):
spaceSecondList.append(" ")
#printing secondList
secondList.extend([operator, " "])
secondList.extend(spaceSecondList)
secondList.extend([secondNum, " "])
#thirdLine:
for eachSpace in range(numWidth):
thirdList.append("-")
thirdList.extend([" "])
#fourthLine [Answer]:
if arg == True:
if operator == "+":
answer = int(firstNum) + int(secondNum)
# print("The sum of equation is:", answer, "and it's length:", len(str(answer)))
elif operator == "-":
answer = int(firstNum) - int(secondNum)
# print("The diff of equation is:", answer, "and it's length:", len(str(answer)))
else:
print("Error: Operator must be '+' or '-'.")
quit()
spaceFourthList = []
for eachSpace in range(numWidth - len(str(answer))):
spaceFourthList.append(" ")
fourthList.extend(spaceFourthList)
fourthList.extend([str(answer)])
# else:
# print("arg is not set to True, therefore no answer is displayed.")
fourthList.extend([" "])
# print("reloop", "\n", "\n")
#Complete Lists
# print("\n", "FirstLine: ",firstList)
# print("\n", "SecondLine: ",secondList)
# print("\n", "thirdLine: ",thirdList)
# print("\n", "fourthLine: ",fourthList)
#String maker + space/"-" remover at end of string
firstString = "".join(firstList).rstrip(" ")
secondString = "".join(secondList).rstrip(" ")
thirdString = "".join(thirdList).rstrip(" ")
fourthString = "".join(fourthList).rstrip(" ")
#Final product
return_statement = firstString + "\n" + secondString + "\n" + thirdString + "\n" + fourthString
print(return_statement)
arithmetic_arranger(["24 + 8525", "301 - 2", "45 + 43", "123 + 49"],True)
the error is because the programn exit without the function returning an output, try fixing this, make so that the function always return something
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
You need not print the output for error, instead you’ve to return it as a string, without exiting the program.
Doing these I managed to pass 4 tests, and next there are two failures:
You’re not checking the operators when answer for the input is not asked(when arg=False)
You’re adding new line to the output string when you need not have to calculate the result(arg=False), remove the newline at end
Thank you a lot for helping me so fast. I’ve corrected my mistakes and handed it in. I’m impressed how you figured out so quickly what the issues were. especially the new line when ( arg == False)… Well done!
I was able to figure out quickly because, I was in your shoes when I was doing this projects. I also had the difficulty designing the output.
When I analyzed your error, I thought of the newline and found the error at exactly same place.
If you don’t mind, you can take a look at my solution for the same problem here
I also want to share a resource, PythonTutor I found this resource in this forum itself, and it’s very much helpful when dealing with this type of problems where aligning is necessary and also in complex scripts, where we need to find the exact cause of error. It’s a kind of debugger.