Tell us what’s happening:
Replit keeps saying theres a syntax error and i can’t figure out where. I though it was in the readme.md and I put a # there and the error went away. Now whenever I click run theres no output whatsoever
Your code so far
def arithmetic_arranger(problems, val=False):
arranged_problems = ''
if len(problems) > 5:
arranged_problems = "Error: Too many problems."
return arranged_problems
# list of all operations in str format
operations = list(map(lambda x: x.split()[1], problems))
if set(operations) != {'+', '-'} and len(set(operations)) != 2:
arranged_problems = "Error: Operator must be '+' or '-'."
return arranged_problems
numbers = [] # list of all operands in str format
for i in problems:
p = i.split()
numbers.extend([p[0], p[2]])
if not all(map(lambda x: x.isdigit(), numbers)):
arranged_problems = "Error: Numbers must only contain digits."
return arranged_problems
if not all(map(lambda x: len(x) < 5, numbers)):
arranged_problems = "Error: Numbers cannot be more than four digits."
return arranged_problems
top_row = ''
dashes = ''
values = list(map(lambda x: eval(x), problems))
solutions = ''
for i in range(0, len(numbers), 2):
space_width = max(len(numbers[i]), len(numbers[i+1])) + 2
top_row += numbers[i].rjust(space_width)
dashes += '-' * space_width
solutions += str(values[i // 2]).rjust(space_width)
if i != len(numbers) - 2:
top_row += ' ' * 4
dashes += ' ' * 4
solutions += ' ' * 4
bottom_row = ''
for i in range(1, len(numbers), 2):
space_width = max(len(numbers[i - 1]), len(numbers[i])) + 1
bottom_row += operations[i // 2]
bottom_row += numbers[i].rjust(space_width)
if i != len(numbers) - 1:
bottom_row += ' ' * 4
if val:
arranged_problems = '\n'.join((top_row, bottom_row, dashes, solutions))
else:
arranged_problems = '\n'.join((top_row, bottom_row, dashes))
return arranged_problems
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15
Challenge: Scientific Computing with Python Projects - Arithmetic Formatter
Link to the challenge: