Tell us what’s happening:
I have done the challenge but i want to check if its correct or now because the console is just showing the same 10 instructions and my prewiew is also not showing anything.
I am using android phone to do this in chrome, so i don’t know if it is happening because of my device…
Im also confused in the if statements
Should i add that parentheses or is is not necessary?
I don’t know how to complete the challanve exactly…
Your code so far
import re
def arithmetic_arranger(problems, show_answers=False):
#if problems more than ⁵ then error
if (len(problems)>5):
return 'Error: Too many problems.'
#if * and % then error and if number operands other than digit then error
for problem in problems:
if (re.search('*','/')):
return "Error: Operation must be '+' or '-'."
elif (re.search('\D')):
return "Error: Numbers must only contain digits."
#if number on each side more than 4 digits then error
first_number = problem.split("")[0]
operator = problem.split("")[1]
second_number = problem.split("")[2]
if (len(first_number)>4 or len(second_number)>4) :
return "Error: Numbers cannot be more than four digits."
#variables for the arrangement
first = ""
second = ""
lines = ""
out = ""
#for calculation
output = ""
if (operator == '+'):
output = str(int(first_number) + int(second_number))
else:
output = str(int(first_number) - int(second_number))
#for the space and dashes
length =max(len(first_number),len(second_number)) + 2
top = str(first_number).rjust(length)
bottom = operator + str(second_number).rjust(length-1)
output_align=str(output).rjust(length)
dash = ""
for i in range(length):
dash += '-'
#the space between two questions
if problem!= problems[-1]:
first += top + ' '
second += bottom + ' '
lines += dash + ' '
out += output_align + ' '
else:
first += top
second += bottom
lines += dash
out += output_align
#now to use the show_answers =False condition for adding space in between
gap =""
if show_answers:
gap = first + '\n' + second + '\n' + lines + '\n' + out
else:
gap = first + '\n' + second + '\n' + out
return gap
Your browser information:
User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Mobile Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project