Tell us what’s happening:
the first thing that is wrong is that my conditionals to check if the string contains any letters doesn’t work.
and the second thing is that my output doesn’t match all the ones that are needed to pass
please help
Your code so far
def arithmetic_arranger(problems, show_answers=False):
top_row = []
bottom_row = []
line_row = []
answer_row = []
errors = [] # Collect errors in a list
if len(problems) > 5:
return 'Error: Too many problems.'
for problem in problems:
split = problem.split()
# Variables
Top_char = split[0]
operator = split[1]
Low_char = split[2]
answer = str(eval(problem))
# Check for length of numbers
if len(Top_char) > 4 or len(Low_char) > 4:
errors.append('Error: Numbers cannot be more than four digits.')
# Check for valid operator
if operator not in ['+', '-']:
errors.append('Error: Operator must be \'+\' or \'-\'.')
# Check if numbers contain non-digits
for char in Top_char:
if not char.isdigit():
errors.append('Error: Numbers must only contain digits.')
break # Break out of the loop on first error
for char in Low_char:
if not char.isdigit():
errors.append('Error: Numbers must only contain digits.')
break # Break out of the loop on first error
if errors:
return errors[0] # Return the first error encountered immediately
# Variables for assembling problem
max_length = max(len(Top_char), len(Low_char)) + 2
top_row.append(Top_char.rjust(max_length))
bottom_row.append(operator + ' ' + Low_char.rjust(max_length - 2))
line_row.append('-' * max_length)
answer_row.append(answer.rjust(max_length))
# Assemble the problem
arranged_problems = " ".join(top_row) + "\n"
arranged_problems += " ".join(bottom_row) + "\n"
arranged_problems += " ".join(line_row) + "\n"
arranged_problems += " ".join(answer_row)
return arranged_problems
print(f'\n{arithmetic_arranger(["213 + 698", "3801 - 2", "45 + 43", "123 - 49"])}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project