Tell us what’s happening:
Can someone please help me get started with the formatting? I’m going through what I learned in the lessons before this project but don’t know what would help with the “space between the operands, the operator on the same line as the second operand, the numbers being right-aligned”.
Your code so far
def arithmetic_arranger(problems, show_answers=False):
#1) Check if there are too many problems supplied
if len(problems) >= 5:
print(f'Error: Too many problems.')
for problem in problems:
parts = problem.split() #The split() method splits the substring "problem" into a list called parts, default separator is any whitespace. Split once, store the result
first_operand = parts[0]
operator = parts[1]
second_operand = parts[2]
#Error checking
if operator not in ["+", "-"]:
print("Error: Operator must be '+' or '-'.")
if not (first_operand.isdigit() and second_operand.isdigit()):
print("Error: Numbers must only contain digits.")
if len(first_operand) > 4 or len(second_operand) > 4:
print('Error: Numbers cannot be more than four digits.')
#Formatting
first_line = ''
second_line = ''
dashes = ''
answer_line = ''
#There should be a single space between the operator and the longest of the two operands, the operator will be on the same line as the second operand, both operands will be in the same order as provided (the first will be the top one and the second will be the bottom).
return problems
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project