Tell us what’s happening:
I did this project in vscode and it runs perfectly, same in the website, the results are correct. but still I couldn’t pass any test, Not even one.
I tried changing some things I thought are causing this like deleting the last 4 spaces from the problems result, yet again still failed all of them and I don’t know why?
Your code so far
#Build an Arithmetic Formatter Project
def validate(string):
for char in string:
find_input = '0123456789 -+*/'
index = find_input.find(char)
#checking for non-digit characters and false operations
if index == -1:
print('Error: Numbers must only contain digits.')
return 'Invalid'
elif index > 12:
#double quotes are preffered because you cant use same quotes inside
print("Error: Operator must be '+' or '-'.")
return 'Invalid'
elif index == 10:
space_index = string.find(char)
reversed_string = string[::-1].find(char)
#accepting 4 digit numbers atmost (max)
if space_index > 4:
print('Error: Numbers cannot be more than four digits.')
return 'Invalid'
elif reversed_string > 4:
print('Error: Numbers cannot be more than four digits.')
return 'Invalid'
def insert_string(string, insert, i = 0, new_string = ''):
#adding a space and dash using iteration according to the lenght of the string
while i > 0:
new_string += insert
i -= 1
new_string += f'{string} '
return new_string
def calculate(top_number, bottom_number, operator):
first_number = int(top_number)
second_number = int(bottom_number)
if operator == '+':
result = first_number + second_number
return f'{result}'
elif operator == '-':
result = first_number - second_number
return f'{result}'
def arithmetic_arranger(problems, show_answers=False):
#string containers to escape the for loop and collect every return value
top_row = ''
bottom_row = '\n'
dash_row = '\n'
answers_row = '\n'
#declining more than five problems
if len(problems) > 5:
print('Error: Too many problems.')
return
else:
for problem in problems:
#removing white space and signs at the start and end
string = problem.strip(' +-')
#validation test:
if validate(string) == 'Invalid':
return
else:
#separating the two operands and getting the operator
space_index = string.find(' ')
operator = string[space_index + 1]
top_number = string[:space_index:]
bottom_number = string[space_index + 1::].strip(' +-')
#calculating the answer...
answer = calculate(top_number, bottom_number, operator)
#formatting the problem display
long_num_space = 2
sign_space = 1
if len(top_number) > len(bottom_number):
dash_num = len(top_number) + long_num_space
short_num_space = dash_num - (len(bottom_number) + sign_space)
top_row += insert_string(top_number, ' ', long_num_space)
bottom_row += insert_string(bottom_number, ' ', short_num_space, operator)
dash_row += insert_string('', '-', dash_num)
answers_row += insert_string(answer, ' ', dash_num - len(answer))
elif len(top_number) == len(bottom_number):
dash_num = len(top_number) + long_num_space
top_row += insert_string(top_number, ' ', long_num_space)
bottom_row += insert_string(bottom_number, ' ', sign_space, operator)
dash_row += insert_string('', '-', dash_num)
if answer[0] != '-':
answers_row += insert_string(answer, ' ', dash_num - len(answer))
else:
answers_row += insert_string(answer[1::], ' ', dash_num - len(answer), answer[0])
elif len(top_number) < len(bottom_number):
dash_num = len(bottom_number) + long_num_space
short_num_space = dash_num - len(top_number)
top_row += insert_string(top_number, ' ', short_num_space)
bottom_row += insert_string(bottom_number, ' ', sign_space, operator)
dash_row += insert_string('', '-', dash_num)
if answer[0] != '-':
answers_row += insert_string(answer, ' ', dash_num - len(answer))
else:
answers_row += insert_string(answer[1::], ' ', dash_num - len(answer), answer[0])
#assigning the containers strings to problems and deleting added space at the end
problems = top_row[:-4:] + bottom_row[:-4:] + dash_row[:-4:]
#displaying answers when the default value of the parameter is True.
if show_answers == True:
problems += answers_row[:-4:]
return print(problems)
arithmetic_arranger(["3801 - 2", "123 + 49", "11 - 44"])```
```py
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project