Tell us what’s happening:
I’m stuck at arithmetic_arranger,i’m very beginner of coding and hope someone can show me what i have to do to solve this.Here my code:
Your code so far
def arithmetic_arranger(problems, statprint=False):
#check problem list
first = ’ ’
second = ’ ’
sumx = ’ ’
lines = ’ ’
#maximal problems is 5
if len(problems) >=6:
return ‘Error: Too many problems.’
#split problem to separate components
for i in(problems):
a = i.split()
firsts = a[0]
seconds = a[2]
operands = a[1]
#check the length of the number,max 4 digits
if (len(firsts) > 4 or len(seconds) > 4):
return “Error: Numbers cannot be more than four digits.”
#check the input as valid digits
if not first.isnumeric() or not second.isnumeric():
return "Error: Numbers must only contain digits."
if (operands == '+' or operands == '-'):
if operands == "+":
sums =str(int(firsts) + int(seconds))
else:
sums = str(int(first) - int(seconds))
#set length of sum and top,bottom and line values
length = max(len(firsts), len(seconds)) + 2
top = str(firsts).rjust(length)
bottom = operands + str(seconds).rjust(length - 1)
line = ''
res = str(sums).rjust(length)
for s in range(length):
line += '-'
#add to the overall string
if i !=problems[-1]:
first += top + ' '
second += bottom + ' '
lines += line + ' '
sumx += res + ' '
else:
first += top
second += bottom
lines += line
sumx += res
return “Error: Operator must be ‘+’ or ‘-’.”
#strip out spaces to the right of the string
first.rstrip()
second.rstrip()
lines.rstrip()
if statprint:
sumx.rstrip()
arranged_problems = first + ‘\n’ + second + ‘\n’ + lines + ‘\n’ + sumx
else:
arranged_problems = first + ‘\n’ + second + “\n” + lines
return arranged_problems
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15
.
Challenge: Arithmetic Formatter
Link to the challenge: