Tell us what’s happening:
So I am working on the first python project and I am really confused. I copy pasted each test case on the main.py file and the display on the console seems to be the asked output.
Although, when I run test_module.py I get 6 failures (the only code that passes being the error messages for wrong user input).
Your code so far
def arithmetic_arranger(problems, get_solution = False ):
if len(problems) > 5 :
return ‘Error: Too many problems.’
# list for each line so each problem get displayed on the same line
l1 =
l2 =
l3 =
l4 =
for problem in problems :
try :
# gets the numbers or error
number_1 = int(problem.split()[0])
number_2 = int(problem.split()[2])
except :
return ‘Error: Numbers must only contain digits.’
# gets the operator or error
if problem.split()[1] == '+' :
operator = problem.split()[1]
solution = number_1 + number_2
elif problem.split()[1] == '-' :
operator = problem.split()[1]
solution = number_1 - number_2
else :
return 'Error: Operator must be \'+\' or \'-\'.'
# gets the length of each number or error
length_1 = len(str(number_1))
length_2 = len(str(number_2))
if max(length_1, length_2) > 4:
return 'Error: Numbers cannot be more than four digits.'
# how many dashes and setting the space between each problem
num_line = max(length_1, length_2) + 2
line = num_line * '-'
space = 4 * ' '
# appends content of each line while adding space + right-aligning
l1.append(f'{number_1:>{num_line}}{space}')
l2.append(f'{operator}{number_2:>{num_line - 1}}{space}')
l3.append(f'{line}{space}')
l4.append(f'{solution:>{num_line}}{space}')
# turns the lists into strings
line_1 = ''.join(l1)
line_2 = ''.join(l2)
line_3 = ''.join(l3)
line_4 = ''.join(l4)
# sets what to print according to the second argument's value
if get_solution :
arranged_problems = line_1 + '\n' + line_2 + '\n' + line_3 + '\n' + line_4
else :
arranged_problems = line_1 + '\n' + line_2 + '\n' + line_3
return arranged_problems
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: