Haven’t been learning coding for long, so any help well be appreciated
def arithmetic_arranger(problems, show):
#breaking list up in to several
top = list()
top_string= ""
bottom=list()
bottom_string=""
op = list()
op_string=""
dash = ""
# Too many problems error
if len(problems)>5:
print(' Error: Too many problems.')
#separate lists and in to top/operator/bottom
for lists in problems:
new = lists.split()
top.append(new[0])
op.append(new[1])
bottom.append(new[2])
top_string.join(top)
bottom_string.join(bottom)
op_string.join(op)
#Caculate and show values. And integer error.
if type(new[0]) != int or type(new[2]) != int:
if show==True and op=="-":
value= int(new[0])-int(new[2])
else:
value= int(new[0])+int(new[2])
else:
print("Error: Numbers must only contain digits.")
# Operator error
if op == "*" or op=="/" in op:
print("Error: Operator must be '+' or '-'.")
# printing dashes
for i in range(len(top_string.rjust(5))):
dash += "-"
# return arranged_problems
if show == True:
arranged_problems = top_string.rjust(5) + "\n" + op_string.rjust(4) + bottom_string.rjust(5) + "\n" + dash + "\n" + str(value)
else:
arranged_problems = top_string.rjust(5) + "\n" + op_string.rjust(1) + bottom_string.rjust(4) + "\n"+ dash
return (arranged_problems)
arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True)
I can’t seem to get the code to work to get the end result.
The question is:
`### Assignment
Students in primary school often arrange arithmetic problems vertically to make them easier to solve. For example, “235 + 52” becomes:
235
+ 52
-----
Create a function that receives a list of strings that are arithmetic problems and returns the problems arranged vertically and side-by-side. The function should optionally take a second argument. When the second argument is set to True, the answers should be displayed.
so I deleted the show argument from the function. However, now I get the following error.
python main.py
-----
FFFEF Error: Too many problems.
F
======================================================================
ERROR: test_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-5/test_module.py", line 63, in test_solutions
actual = arithmetic_arranger(
TypeError: arithmetic_arranger() takes 1 positional argument but 2 were given
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-5/test_module.py", line 11, in test_arrangement
self.assertEqual(
AssertionError: ' \n \n-----' != ' 3 3801 45 123\n+ 855 - [51 chars]----'
-
-
- -----+ 3 3801 45 123
+ + 855 - 2 + 43 + 49
+ ----- ------ ---- ----- : 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-5/test_module.py", line 39, in test_incorrect_operator
self.assertEqual(
AssertionError: ' \n \n-----' != "Error: Operator must be '+' or '-'."
+ 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-5/test_module.py", line 57, in test_only_digits
self.assertEqual(
AssertionError: ' \n \n-----' != 'Error: Numbers must only contain digits.'
+ 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_too_many_digits (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-5/test_module.py", line 48, in test_too_many_digits
self.assertEqual(
AssertionError: ' \n \n-----' != 'Error: Numbers cannot be more than four digits.'
+ 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-5/test_module.py", line 30, in test_too_many_problems
self.assertEqual(
AssertionError: ' \n \n-----' != 'Error: Too many problems.'
+ Error: Too many problems.-
-
- ----- : Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."
----------------------------------------------------------------------
Ran 6 tests in 0.024s
FAILED (failures=5, errors=1)
as I said, the second argument needs to be optional
now when the function is called as arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"], True) you get that error because you have only one function parameter
I think I understood, and I wasn’t clear enough before. I removed the show argument from the function call. But it still gives the same error.
def arithmetic_arranger(problems):
#breaking list up in to several
top = list()
top_string= ""
bottom=list()
bottom_string=""
op = list()
op_string=""
dash = ""
# Too many problems error
if len(problems)>5:
print(' Error: Too many problems.')
#separate lists and in to top/operator/bottom
for lists in problems:
new = lists.split()
top.append(new[0])
op.append(new[1])
bottom.append(new[2])
top_string.join(top)
bottom_string.join(bottom)
op_string.join(op)
#Caculate and show values. And integer error.
if type(new[0]) != int or type(new[2]) != int:
if op=="-":
value= int(new[0])-int(new[2])
else:
value= int(new[0])+int(new[2])
else:
print("Error: Numbers must only contain digits.")
# Operator error
if (op == "*" or op=="/") in op:
print("Error: Operator must be '+' or '-'.")
# printing dashes
for i in range(len(top_string.rjust(5))):
dash += "-"
# return arranged_problems
arranged_problems = top_string.rjust(5) + "\n" + op_string.rjust(1) + bottom_string.rjust(4) + "\n"+ dash
return arranged_problems
So I realised how much it all sucked. I deleted it and started again since.
In Vscode it works perfectly fine and outputs the required values in the right format. However, on repl.it it keeps coming up with failures, and I don’t know what mistake I have made - I have tried to debug myself numerous times.
My code:
def arithmetic_arranger(problems, show=False):
# Too many problems error
if len(problems)>4:
print('Error: Too many problems.')
else:
#separate lists and in to top/operator/bottom
one = ""
two=""
three=""
test=list()
top=list()
top1=""
final_values=list()
dashes=list()
for lists in problems:
new = lists.split()
one = new[0]
two = new[1]
three = new[2]
#Caculate and show values. And integer error.
if one.isnumeric()==False or three.isnumeric()==False:
return ("Error: Numbers must only contain digits.")
elif len(one) >4 or len(three)>4:
return ("Error: Numbers cannot be more than four digits.")
# Operator error
elif "/" in two or "*" in two:
return ("Error: Operator must be '+' or '-'.")
else:
if two=="-":
value= int(one)-int(three)
else:
value= int(one)+int(three)
#width adjustment
if len(one)>=len(three):
width = len(one)
else:
width=len(three)
#Append numbers to top and bottom lists
top.append(one.rjust(width+2))
two_three = two.rjust(1) + three.rjust(width+1)
test.append(two_three)
final_values.append(str(value).rjust(width+2))
#Adding dashed lines
if len(one)>=len(three):
dash = (len(one)+2)*"-"
dashes.append((dash).rjust(width))
else:
dash = len(two_three)*"-"
dashes.append((dash).rjust(width))
#convert appended lists to strings
top_string=" ".join(str(x) for x in top)
middle_string=" ".join(str(x) for x in test)
final_value_string=" ".join(str(x) for x in final_values)
dashes_string=" ".join(str(x) for x in dashes)
# return arranged_problems
if show == True:
print(top_string+"\n"+ middle_string+"\n"+dashes_string+"\n"+final_value_string)
else:
print(top_string+"\n"+ middle_string+"\n"+dashes_string)
return
The error messages I get on repl.it:
python main.py
32 3801 45 123
+ 698 - 2 + 43 + 49
----- ------ ---- -----
None
3 3801 45 123
+ 855 - 2 + 43 + 49
----- ------ ---- -----
F.. 32 1 45 123
- 698 - 3801 + 43 + 49
----- ------ ---- -----
-666 -3800 88 172
F.Error: Too many problems.
F
======================================================================
FAIL: test_arrangement (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-7/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_solutions (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-7/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_problems (test_module.UnitTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/runner/boilerplate-arithmetic-formatter-7/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.005s
FAILED (failures=3)
I think it’s something to do with additional spaces being added somewhere, but I’m unsure how or why.
Ah yeah. I’ve amended it and it’s knocked my failures down to two. Now I get the following:
def arithmetic_arranger(problems, show=False):
# Too many problems error
if len(problems)>4:
return('Error: Too many problems.')
else:
#separate lists and in to top/operator/bottom
one = ""
two=""
three=""
test=list()
top=list()
top1=""
final_values=list()
dashes=list()
for lists in problems:
new = lists.split()
one = new[0]
two = new[1]
three = new[2]
#Caculate and show values. And integer error.
if one.isnumeric()==False or three.isnumeric()==False:
return ("Error: Numbers must only contain digits.")
elif len(one) >4 or len(three)>4:
return ("Error: Numbers cannot be more than four digits.")
# Operator error
elif "/" in two or "*" in two:
return ("Error: Operator must be '+' or '-'.")
else:
if two=="-":
value= int(one)-int(three)
else:
value= int(one)+int(three)
#width adjustment
if len(one)>=len(three):
width = len(one)
else:
width=len(three)
#Append numbers to top and bottom lists
top.append(one.rjust(width+2))
two_three = two.rjust(1) + three.rjust(width+1)
test.append(two_three)
final_values.append(str(value).rjust(width+2))
#Adding dashed lines
if len(one)>=len(three):
dash = (len(one)+2)*"-"
dashes.append((dash).rjust(width))
else:
dash = len(two_three)*"-"
dashes.append((dash).rjust(width))
#convert appended lists to strings
top_string=" ".join(str(x) for x in top)
middle_string=" ".join(str(x) for x in test)
final_value_string=" ".join(str(x) for x in final_values)
dashes_string=" ".join(str(x) for x in dashes)
# return arranged_problems
if show == True:
return (top_string+"\n"+ middle_string+"\n"+dashes_string+"\n"+final_value_string)
else:
return (top_string+"\n"+ middle_string+"\n"+dashes_string)