Tell us what’s happening:
Describe your issue in detail here.
While running the program or code that I created for this assignment, I am getting 6 assertion errors as per the test_module.py.
The errors with the following ids are displayed.
test_two_problems_arrangement1
test_two_problems_arrangement2
test_four_problems_arrangement
test_five_problems_arrangement
test_two_problems_with_solutions
test_five_problems_with_solutions
I really can’t make out what exactly these errors are pointing out. It could be the formatting but without knowing specifically what, I don’t know where to make the changes. As far as the spacing requirements (btw the values and problems) are concerned, I believe it has been taken care of. Please advise!
Your code so far
def arithmetic_arranger(myinput, showresult=False) :
myOperand1 = []
myOperand2 = []
myOpr = []
count = 0
for inputs in myinput:
myList = inputs.split(" ")
count += 1
try:
if isinstance(int(myList[0]), int) & isinstance(int(myList[2]), int):
myOperand1.append(myList[0])
myOperand2.append(myList[2])
except ValueError:
return "Error: Numbers must only contain digits."
if myList[1] == "+" or myList[1] == "-":
myOpr.append((myList[1]))
else:
return "Error: Operator must be '+' or '-'."
if count > 5:
return "Error: Too many problems."
if len(myList[0]) > 4 or len(myList[2]) > 4:
return "Error: Numbers cannot be more than four digits."
# print all the first operands with the required spacing
for i in range(len(myOperand1)):
if len(myOperand1[i]) <= len(myOperand2[i]):
print(myOperand1[i].rjust(len(myOperand2[i]) + 2) + " ",end=" ")
else:
print(myOperand1[i].rjust(len(myOperand1[i]) + 2) + " ", end=" ")
print()
# print the operators and the second operands with the required spacing
for i in range(len(myOperand2)):
if len(myOperand1[i]) <= len(myOperand2[i]):
print(myOpr[i] + " " + myOperand2[i].rjust(len(myOperand2[i])) + " ",end=" ")
else:
print(myOpr[i] + " " + myOperand2[i].rjust(len(myOperand1[i])) + " ", end=" ")
print()
# print the dash ----
for i in range(len(myOperand2)):
if len(myOperand1[i]) <= len(myOperand2[i]):
num = len(myOperand2[i]) + 2
print(num * "-" + " ",end=" ")
else:
num = len(myOperand1[i]) + 2
print(num * "-" + " ", end=" ")
print()
# print the sum or difference btw the operands as per the operator
if showresult:
for i in range(len(myOperand2)):
if myOpr[i] == "+":
sum = int(myOperand1[i]) + int(myOperand2[i])
if len(myOperand1[i]) <= len(myOperand2[i]):
print(str(sum).rjust(len(myOperand2[i]) + 2) + " ", end=" ")
else:
print(str(sum).rjust(len(myOperand1[i]) + 2) + " ", end=" ")
else:
sum = int(myOperand1[i]) - int(myOperand2[i])
if len(myOperand1[i]) <= len(myOperand2[i]):
print(str(sum).rjust(len(myOperand2[i]) + 2) + " ", end=" ")
else:
print(str(sum).rjust(len(myOperand1[i]) + 2) + " ", end=" ")
return "Completed"
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36
Thank you for replying. Are you sure that I shouldn’t be using print anywhere in the function to output the results. Wish FCC had clearly mentioned about it in the requirements.
As such, using print (and all the same code) is giving me the correct formatted results when i tried the code separately on my IDE.
def arithmetic_arranger(myinput, showresult=False):
myOperand1 = []
myOperand2 = []
myOpr = []
count = 0
outputStr = ""
for inputs in myinput:
myList = inputs.split(" ")
count += 1
try:
if isinstance(int(myList[0]), int) & isinstance(
int(myList[2]), int):
myOperand1.append(myList[0])
myOperand2.append(myList[2])
except ValueError:
return "Error: Numbers must only contain digits."
if myList[1] == "+" or myList[1] == "-":
myOpr.append((myList[1]))
else:
return "Error: Operator must be '+' or '-'."
if count > 5:
return "Error: Too many problems."
if len(myList[0]) > 4 or len(myList[2]) > 4:
return "Error: Numbers cannot be more than four digits."
# Save all the first operands with the required spacing in a string
for i in range(len(myOperand1)):
if len(myOperand1[i]) <= len(myOperand2[i]):
outputStr += myOperand1[i].rjust(len(myOperand2[i]) + 2) + " "
else:
outputStr += myOperand1[i].rjust(len(myOperand1[i]) + 2) + " "
outputStr += "\n"
# save the operators and the second operands with the required spacing in to the string
for i in range(len(myOperand2)):
if len(myOperand1[i]) <= len(myOperand2[i]):
outputStr += myOpr[i] + " " + myOperand2[i].rjust(
len(myOperand2[i])) + " "
else:
outputStr += myOpr[i] + " " + myOperand2[i].rjust(
len(myOperand1[i])) + " "
outputStr += "\n"
# print the dashes ----
for i in range(len(myOperand2)):
if len(myOperand1[i]) <= len(myOperand2[i]):
num = len(myOperand2[i]) + 2
outputStr += num * "-" + " "
else:
num = len(myOperand1[i]) + 2
outputStr += num * "-" + " "
outputStr += "\n"
# print the sum or difference btw the operands as per the operator
if showresult:
for i in range(len(myOperand2)):
if myOpr[i] == "+":
sum = int(myOperand1[i]) + int(myOperand2[i])
if len(myOperand1[i]) <= len(myOperand2[i]):
outputStr += str(sum).rjust(len(myOperand2[i]) +
2) + " "
else:
outputStr += str(sum).rjust(len(myOperand1[i]) +
2) + " "
else:
sum = int(myOperand1[i]) - int(myOperand2[i])
if len(myOperand1[i]) <= len(myOperand2[i]):
outputStr += str(sum).rjust(len(myOperand2[i]) +
2) + " "
else:
outputStr += str(sum).rjust(len(myOperand1[i]) +
2) + " "
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.
Did you see the extra 10 spaces I put at the end of that previous line? You are adding 4 extra spaces to the end of each line.
It also doesn’t look like you are creating a multi line output with each result side by side? Oh, you have many loops. You still have extra spaces though.
Providing a link to your repl makes this much easier.
As per the requirement " There should be four spaces between each problem". This is why I specifically added 4 spaces after every operand.
The way I have created this, I am saving all the first operands in the string first with required spaces and then the operator and all the second operands.
It is affecting the output… The extra spaces are wrong. Since the extra spaces are not part of the specification, your string fails to match the expected output.