My code seems to be having some issues that I don’t know how to fix… any help is appreciated!
My code:
def arithmetic_arranger(problems, opt=False):
# Creating dictonaries
firstnumber = list()
operator = list()
lastnumber = list()
# Checking number of problems
if len(problems) > 5 :
print ("Error: Too many problems.")
for problem in problems:
# Checking number of problems
if len(problems) >= 6 :
print ("Error: Too many problems.")
continue
# Find first number space
ffnspace = problem.find(" ")
intffnspace = int(ffnspace)
ffn = problem[0:intffnspace]
try:
intffn = int(ffn)
firstnumber.append(intffn)
except:
print("\n", "Error: Numbers must only contain digits.")
continue
# find second number space
fsnspace = ffnspace + 3
intfsnspace = int(fsnspace)
fsn = problem[fsnspace:]
try:
intfsn = int(fsn)
lastnumber.append(intfsn)
except:
print("\n", "Error: Numbers must only contain digits.")
continue
# finding operator
fospace = ffnspace + 1
intfo = int(fospace)
fo = problem[intfo]
operator.append(fo)
def arranged_problems():
count = 0
for number in firstnumber:
newline = str(firstnumber[count]).rjust(5)
count = count + 1
print(newline, end=" ")
count = 0
for op in operator:
newline = operator[count]
count = count + 1
if count == 4:
print(newline, end="")
print(str(lastnumber[count - 1]).rjust(4), end="")
elif count == 3:
print(newline, end="")
print(str(lastnumber[count - 1]).rjust(4), end=" "*4)
elif count == 2:
print(newline, end="")
print(str(lastnumber[count - 1]).rjust(4), end=" "*4)
elif count == 1:
print("\n", end="")
newnewline = newline.ljust(2)
print(newnewline, end="")
print(str(lastnumber[count - 1]).rjust(3), end=" "*4)
count = 0
for number in firstnumber:
newline = "-----"
count = count + 1
if count == 1:
print("\n", end="")
print(newline, end=" ")
else:
print(newline, end=" ")
return arranged_problems()
The output is the following:
python main.py
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ----- ----- ----- None
3 3801 45 123
+ 855 - 2 + 43 + 49
F----- ----- ----- ----- 3 3801 45 123
/ 855 - 2 + 43 + 49
F----- ----- ----- -----
Error: Numbers must only contain digits.
98 3801 45 123
- 2 + 43 + 49
F----- ----- ----- ----- 32 1 45 123
- 698 -3801 + 43 + 49
F----- ----- ----- ----- 24 3801 45 123
+ 85215 - 2 + 43 + 49
F----- ----- ----- ----- Error: Too many problems.
Error: Too many problems.
Error: Too many problems.
Error: Too many problems.
Error: Too many problems.
Error: Too many problems.
Error: Too many problems.
F
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 10, in test_arrangement
self.assertEqual(actual, expected, 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]')
AssertionError: None != ' 3 3801 45 123\n+ 855 [56 chars]----' : Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]
======================================================================
FAIL: test_incorrect_operator (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 24, in test_incorrect_operator
self.assertEqual(actual, expected, '''Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."''')
AssertionError: None != "Error: Operator must be '+' or '-'." : Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be '+' or '-'."
======================================================================
FAIL: test_only_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 34, in test_only_digits
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."')
AssertionError: None != 'Error: Numbers must only contain digits.' : Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."
======================================================================
FAIL: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 39, in test_solutions
self.assertEqual(actual, expected, 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.')
AssertionError: None != ' 32 1 45 123\n- 698 [90 chars] 172' : Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with arithemetic problems and a second argument of `True`.
======================================================================
FAIL: test_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 29, in test_too_many_digits
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."')
AssertionError: None != 'Error: Numbers cannot be more than four digits.' : Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."
======================================================================
FAIL: test_too_many_problems (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter/test_module.py", line 19, in test_too_many_problems
self.assertEqual(actual, expected, 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."')
AssertionError: None != 'Error: Too many problems.' : Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."
----------------------------------------------------------------------
Ran 6 tests in 0.066s
FAILED (failures=6)

, I was really lost…