Tell us what’s happening:
The code works fine, but, for some reason, it isn’t enough.i don’t really know what’s happening. Please, help. I know is not de better code, but it works.
Your code so far
def calculadora():
pass
def arithmetic_arranger(problems, show_answers=False):
if len(problems)>5:
raise ValueError('Error: Too many problems')
else:
first_line=''
second_line=''
barra_line=''
for cuenta in problems:
operand1=[]
operand2=[]
sig=''
barra='--'
tradu_cuenta=str.maketrans({' ':''})
cuenta_final=cuenta.translate(tradu_cuenta)
for digit in cuenta_final:
if digit.isdigit():
pass
elif digit=='+'or digit=='-':
sig=digit
else:
if digit.isalpha():
raise ValueError('Error: Numbers must only contain digits.')
else:
raise ValueError("Error: Operator must be '+' or '-'.")
cuenta_final=cuenta_final.split(sig)
operand1=list(cuenta_final[0])
operand2=list(cuenta_final[1])
if len(operand1)>4 or len(operand2)>4:
raise ValueError('Error: Numbers cannot be more than four digits.')
while len(operand1)<len(operand2):
operand1.insert(0,'_')
while len(operand2)<len(operand1):
operand2.insert(0,'_')
operand1.insert(0,'__')
for _ in operand2:
barra+='-'
operando_1=''.join(operand1).replace('_',' ')
operando_2=''.join(operand2).replace('_',' ')
first_line+=operando_1+' '
second_line+=sig+' '+operando_2+' '
barra_line+=barra+' '
first_line=first_line.removesuffix(' ')
second_line='\n'+second_line
barra_line='\n'+barra_line
problems=first_line+second_line+barra_line
return problems
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 OPR/114.0.0.0
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project