Tell us what’s happening:
Hii! I have a problem with my project. For me it seems everything is okay, but when it comes to running tests, there is something wrong. I don’t know what is the problem. I know that my idea for formatting it is rather strange, i guess i should use “:>number” (although output looks perfectly fine. And I really do not want to do it again.). Could my formatting be a big problem? Or something else?
(Validation tests are okay, btw)
Your code so far
def validation(problems):
if len(problems) > 5:
return "Error: Too many problems."
for problem in problems:
element = problem.split(" ")
if len(element) > 3: # sprawdza, czy każdy problem zawiera trzy elementy: dwie liczby i operator
return "Error: You should have two numbers and operator. Check you spelling."
if element[1] not in ["+", "-"]:
return "Error: Operator must be '+' or '-'."
if not element[0].isdigit() or not element[2].isdigit():
return "Error: Numbers must only contain digits."
if len(element[0]) > 4 or len(element[2]) > 4:
return 'Error: Numbers cannot be more than four digits.'
def problem_arranged_vertically(problems):
# Stworzenie zmiennych, które będą przechowywać dolne i górne części równania
left_operand = []
operator_and_right_operand = []
# Ta pętla dodaje odpowiednie wartości do left_operand i operator_and_right_operand, oraz dba o to, by odległość między rówaniami wynosiła 4 spacje
for problem in problems:
problem_parts = problem.split(" ")
# Dzięki temu linie są równe
# Dzięki temu odległość między każdym równaniem wynosi 4 spacje
space0 = ""
space = ""
if len(problem_parts[0]) > len(problem_parts[2]):
space0 = " "
space_needed = len(problem_parts[0]) - len(problem_parts[2])
space = (" " * space_needed) + " "
elif len(problem_parts[0]) < len(problem_parts[2]):
space_needed0 = len(problem_parts[2]) - len(problem_parts[0])
space0 = " " + (" " * space_needed0)
space = " "
elif len(problem_parts[0]) == len(problem_parts[2]):
space0 = " "
space = " "
# Dodanie wartości do górnej części
left_operand.append(space0 + problem_parts[0] + " ")
# Dodanie wartości do dolnej części
operator_and_right_operand.append(problem_parts[1] + space + problem_parts[2] + " ")
arranged_problems = ""
# Pętla dla górnych wartości
for left in left_operand:
arranged_problems += f"{left}"
arranged_problems += f"\n"
# Pętla dla dolnych wartości
for formatted_problem in operator_and_right_operand:
arranged_problems += f"{formatted_problem}"
arranged_problems += f"\n"
# Wstępna wersja pętli dla ---
for formatted_problem in operator_and_right_operand:
line_length = len(formatted_problem) - 4
dashes = "-" * line_length
arranged_problems += dashes + " "
return arranged_problems.rstrip()
def show_solutions(problems):
solutions = ""
for problem in problems:
problem_parts = problem.split(" ")
if "+" in problem:
solution = int(problem_parts[0]) + int(problem_parts[2])
elif "-" in problem:
solution = int(problem_parts[0]) - int(problem_parts[2])
solution_str = str(solution)
spaces_first = ""
if len(problem_parts[0]) < len(problem_parts[2]):
spaces_first = " " * (len(problem_parts[2]) + 2 - len(solution_str))
elif len(problem_parts[0]) >= len(problem_parts[2]):
spaces_first = " " * (len(problem_parts[0]) + 2 - len(solution_str))
spaces_second = " "
solutions += spaces_first
solutions += solution_str
solutions += spaces_second
return solutions.rstrip()
def arithmetic_arranger(problems, show_answers=False):
error = validation(problems)
if error:
return error
what_to_return = problem_arranged_vertically(problems)
if show_answers:
what_to_return += "\n" + show_solutions(problems)
return what_to_return
else:
return what_to_return
print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project