i can’t pass the test even if my code works, and also i used repr()
can anyone help?
Your code so far
def arithmetic_arranger(problems, display_answers=False):
# error with * and /
first_numbers = []
second_numbers = []
operators = []
answers = []
for problem in problems:
if '+' in problem:
operator = '+'
parts = problem.split('+')
elif '-' in problem:
operator = '-'
parts = problem.split('-')
else:
raise ValueError("Error: Operator must be '+' or '-'.")
first = parts[0].strip()
second = parts[1].strip()
#error with 4 digits
if len(first) > 4 or len(second) > 4:
raise ValueError("Error: Numbers cannot be more than four digits.")
first_numbers.append(first)
second_numbers.append(second)
operators.append(operator)
#error with digits
alowed_chars=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', ' ']
for problem in problems:
for digit in problem:
if digit not in alowed_chars:
raise ValueError('Error: Numbers must only contain digits.')
# Check digit lengths
if not first.isdigit() or not second.isdigit():
raise ValueError("Error: Numbers must only contain digits.")
#lot of problems
if len(problems)>5:
raise ValueError('Error: Too many problems.')
#output
top = []
bottom = []
dashes = []
answer_row = []
for i in range(len(problems)):
width = max(len(first_numbers[i]), len(second_numbers[i])) + 2
top.append(first_numbers[i].rjust(width))
bottom.append(operators[i] + ' ' + second_numbers[i].rjust(width - 2))
dashes.append('-' * width)
if display_answers:
if operators[i] == '+':
answer = str(int(first_numbers[i]) + int(second_numbers[i]))
else:
answer = str(int(first_numbers[i]) - int(second_numbers[i]))
answer_row.append(answer.rjust(width))
spaces=(' '.join(top) + '\n' + ' '.join(bottom) + '\n'+' '.join(dashes))
if display_answers:
spaces = spaces + ('\n' +' '.join(answer_row))
return spaces
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project
Using repr() as you have done above. You’ll need to clearly label each.
Try to think about how to present what information is needed and how to present it in a legible way. Not only will this help people to help you on the forum, it will help you solve the problem yourself.
the test 5 says that when i enter more than 4 problems it should raise an error, and when i did it, my code show me the error, which means that my code is working, but not passing the test
You need to take the tests extremely literally. It actually does not say you should raise an error. It says your function should return a string. You need to make sure what your function returns is exactly, word for word, spaces and punctuation what is written.
def arithmetic_arranger(problems, display_answers=False):
# error with * and /
first_numbers =
second_numbers =
operators =
answers =
for problem in problems:
if '+' in problem:
operator = '+'
parts = problem.split('+')
elif '-' in problem:
operator = '-'
parts = problem.split('-')
else:
print("Error: Operator must be '+' or '-'.")
first = parts[0].strip()
second = parts[1].strip()
#error with 4 digits
if len(first) > 4 or len(second) > 4:
print('Error: Numbers cannot be more than four digits.')
first_numbers.append(first)
second_numbers.append(second)
operators.append(operator)
#error with digits
alowed_chars=['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', ' ']
for problem in problems:
for digit in problem:
if digit not in alowed_chars:
print('Error: Numbers must only contain digits.')
#number of digits
if len(first) > 4 or len(second) > 4:
print(‘Error: Numbers cannot be more than four digits.’)
#lot of problems
if len(problems)>5:
print('Error: Too many problems.')
#output
top = []
bottom = []
dashes = []
answer_row = []
for i in range(len(problems)):
width = max(len(first_numbers[i]), len(second_numbers[i])) + 2
top.append(first_numbers[i].rjust(width))
bottom.append(operators[i] + ' ' + second_numbers[i].rjust(width - 2))
dashes.append('-' * width)
if display_answers:
if operators[i] == '+':
answer = str(int(first_numbers[i]) + int(second_numbers[i]))
else:
answer = str(int(first_numbers[i]) - int(second_numbers[i]))
answer_row.append(answer.rjust(width))
spaces=(' '.join(top) + '\n' + ' '.join(bottom) + '\n'+' '.join(dashes))
if display_answers:
spaces = spaces + ('\n' +' '.join(answer_row))
return spaces
i finallt knew my fault, it was the raise error that i should replace it with a return, Thank you so much for helping me, and sorry if i cause you problems.
I am having issue with the tests, which are failing somehow.
but when i am running the same test data on the function i created i am getting the right error messages:
hi @abhishekg00715 , please create your own topic, we are unable to help you in someone else’s topic
If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.
The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.