seyjo
September 22, 2022, 8:14pm
1
Hello guys,
im desperatly trying to solve the Arithmetic Formatter…
im getting 10 of 10 failes, but if i check the first line and all the tries in my output, it looks exactly as it should.
I even checked other solutions to find somewhere a clue but i really have no idea what’s wrong…
help or a little hint would be great.
Best
seyjo
you can find my code here:
https://replit.com/@seyjo/boilerplate-arithmetic-formatter-2#arithmetic_arranger.py
or here:
summe=""
arranged_problems = ""
def arithmetic_arranger(problems,solve=True):
#error if too many probs
if len(problems)>5:
return "Error, Too many problems."
value1 =""
value2 =""
line =""
solution=""
arranged_problems =""
for problem in problems:
new_list=problem.split()
row1 = new_list[0]
operator = new_list[1]
row2 = new_list[2]
# only + and -
if (operator !="+" or operator !="-"):
pass
else:
return "Error: Operator must be '+' or '-'."
# only numbers
if row1.isnumeric()==True and row2.isnumeric()==True:
pass
else:
return "Error: Numbers must be only contain digits."
# max 4 digits
if (len(row1)>5 or len(row2)>4):
return "Error Numbers cannot be more than four digits."
#calculation
summe = eval(problem)
#create line:
line_length = max(len(row1),len(row2)) +2
line_n =""
for leng in range(line_length):
line_n = line_n + "-"
#align operator and row2 line
oprow = operator + row2.rjust(line_length-1)
#align and define numrow
numrow = row1.rjust(line_length)
#align summe as sumrow
sumrow = str(summe).rjust(line_length)
#print(numrow)
#glue everything together
value1 = value1 + numrow + " "
value2= value2 + oprow + " "
line = line + line_n + " "
solution = solution + sumrow + " "
#print("x")
#arranged_problems = (value1,value2, line, solution+ "\n")
if solve == True:
arranged_problems = value1 + "\n" + value2 + "\n" + line + "\n" + solution
else:
arranged_problems = value1 + "\n" + value2 + "\n" + line + "\n"
#print(arranged_problems)
return arranged_problems
#arithmetic_arranger(["3801 - 2", "123 + 49"])
#print(arranged_problems)
Always adding space will cause a problem
Double check punctuation and capitalization for your error messages - it must exactly, perfectly match
seyjo
September 22, 2022, 8:24pm
3
ok that was fast, thank you for your answer.
the first:
Don’t get it, how to set the space? i tried tabs, more and more less space.
tabs produce uneven spaces…
second:
thank you very much, now i passed 9/10
You need to use spaces, but not after every single problem. You shouldn’t have spaces after the rightmost problem.
seyjo
September 22, 2022, 8:54pm
5
ok, got it, thank you very much, makes complete sense.
i rechecked my code and let it run, got 6/4.
what ´s strange is, i passed the problems with solutions but not the problems arranged…:
Blockquote
test_module.py::test_template[test_two_problems_arrangement1] FAILED [ 10%]
test_module.py::test_template[test_two_problems_arrangement2] FAILED [ 20%]
test_module.py::test_template[test_four_problems_arrangement] FAILED [ 30%]
test_module.py::test_template[test_five_problems_arrangement] FAILED [ 40%]
test_module.py::test_template[test_too_many_problems] PASSED [ 50%]
test_module.py::test_template[test_incorrect_operator] PASSED [ 60%]
test_module.py::test_template[test_too_many_digits] PASSED [ 70%]
test_module.py::test_template[test_only_digits] PASSED [ 80%]
test_module.py::test_template[test_two_problems_with_solutions] PASSED [ 90%]
test_module.py::test_template[test_five_problems_with_solutions] PASSED [100%]
Blockquote
current code (is refreshed in replit as well):
and please only some hints, as before, like to learn
summe=""
arranged_problems = ""
def arithmetic_arranger(problems,solve=True):
#error if too many probs
if len(problems)>5:
return "Error: Too many problems."
value1 =""
value2 =""
line =""
solution=""
arranged_problems =""
for problem in problems:
new_list=problem.split()
row1 = new_list[0]
operator = new_list[1]
row2 = new_list[2]
# only + and -
if (operator =="+" or operator =="-"):
pass
else:
return "Error: Operator must be '+' or '-'."
# only numbers
if row1.isnumeric()==True and row2.isnumeric()==True:
pass
else:
return "Error: Numbers must only contain digits."
# max 4 digits
if (len(row1)>4 or len(row2)>4):
return "Error: Numbers cannot be more than four digits."
#calculation
summe = eval(problem)
#create line:
line_length = max(len(row1),len(row2)) +2
line_n =""
for leng in range(line_length):
line_n = line_n + "-"
#align operator and row2 line
oprow = operator + row2.rjust(line_length-1)
#align and define numrow
numrow = row1.rjust(line_length)
#align summe as sumrow
sumrow = str(summe).rjust(line_length)
#print(numrow)
#glue everything together
if problem == problems[-1]:
value1 = value1 + numrow
value2= value2 + oprow
line = line + line_n
solution = solution + sumrow
else:
value1 = value1 + numrow + " "
value2= value2 + oprow + " "
line = line + line_n + " "
solution = solution + sumrow + " "
#print("x")
#arranged_problems = (value1,value2, line, solution+ "\n")
if solve == True:
arranged_problems = value1 + "\n" + value2 + "\n" + line + "\n" + solution
else:
arranged_problems = value1 + "\n" + value2 + "\n" + line + "\n"
#print(arranged_problems)
return arranged_problems
#arithmetic_arranger(["3801 - 2", "123 + 49"])
#print(arranged_problems)
- ------ -----
+ ------ -----
? +
- --- ------
+ --- ------
? +
- ----- ------ ---- -----
+ ----- ------ ---- -----
? +
- ---- ------ --- ----- ------
+ ---- ------ --- ----- ------
? +
These errors are saying you have an extra character after your line of dashes.
system
Closed
March 24, 2023, 9:51am
7
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.