While running the test, the output screen is turning up blank. There isn’t any hint of errors since nothing is reflected on the preview. Why is this happening?
Your code so far
def arithmetic_arranger(problems, show_answers=True):
problems=""
if len(problems)>5:
return 'Error: Too many problems.'
else:
for problem in problems:
if problems.find("*") or problems.find("/"):
return "Error: Operator must be '+' or '-'."
else:
first_number= problems.split()[0]
operator=problems.split()[1]
second_number= problems.split()[2]
if first_number.isdigit() and second_number.isdigit():
if len(first_number)>4 and len(second_number)>4:
return 'Error: Numbers cannot be more than four digits.'
else:
return "Error: Numbers must only contain digits."
length= max(len(first_number), len(second_number))
sum=""
if operator=='+':
sum= str(int(first_number)+ int(second_number))
else:
sum= str(int(first_number)-int(second_number))
top=first_number.rjust(length)
bottom= operators + str(second_number.rjust(length - 1))
dashline=""
result= str(sum).rjust(length)
for dashes in range(length):
dashline += "-"
if problem != problems[-1]:
first += top+' '
second += bottom+' '
dashlines+= dashline+ ' '
results+= result+' '
else:
first += top
second += bottom
dashlines+= dashline
results+= result
if show_answers:
real_problem= first + '\n' + second + '\n' + dashlines + '\n' + results
else:
real_problem = first + '\n' + second + '\n' + dashlines
return real_problem
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project
Now the word ‘none’ is being printed on the terminal.
def arithmetic_arranger(problems, show_answers=True):
problems=""
if len(problems)>5:
return 'Error: Too many problems.'
else:
for problem in problems:
if problems.find("*") or problems.find("/"):
return "Error: Operator must be '+' or '-'."
else:
first_number= problems.split()[0]
operator=problems.split()[1]
second_number= problems.split()[2]
if first_number.isdigit() and second_number.isdigit():
if len(first_number)>4 and len(second_number)>4:
return 'Error: Numbers cannot be more than four digits.'
else:
return "Error: Numbers must only contain digits."
length= max(len(first_number), len(second_number))
sum=""
if operator=='+':
sum= str(int(first_number)+ int(second_number))
else:
sum= str(int(first_number)-int(second_number))
top=first_number.rjust(length)
bottom= operators + str(second_number.rjust(length - 1))
dashline=""
result= str(sum).rjust(length)
for dashes in range(length):
dashline += "-"
if problem != problems[-1]:
first += top+' '
second += bottom+' '
dashlines+= dashline+ ' '
results+= result+' '
else:
first += top
second += bottom
dashlines+= dashline
results+= result
if show_answers:
realproblem= first + '\n' + second + '\n' + dashlines + '\n' + results
else:
realproblem = first + '\n' + second + '\n' + dashlines
return realproblem
print(arithmetic_arranger(["3801 - 2", "123 + 49"]))
I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
Ive made a couple of changes to the code and now it is passing tests 5,6,7, and 8, but in the other tests the function is not reading beyond the first arithmetic expression. How should I make the code read the other arithmetic expressions in the parameter?
def arithmetic_arranger(problems, show_answers=False):
if len(problems)>5:
return 'Error: Too many problems.'
else:
for problem in problems:
first_number= problem.split()[0]
operator=problem.split()[1]
second_number= problem.split()[2]
if first_number.isdigit() and second_number.isdigit():
if len(first_number)<5 and len(second_number)<5: #rbfhrfrfhrbfh
if operator=='+' or operator=='-':
length= max(len(first_number), len(second_number))
sum=""
if operator=='+':
sum= str(int(first_number)+ int(second_number))
else:
sum= str(int(first_number)-int(second_number))
top=first_number.rjust(length)
bottom= operator + str(second_number.rjust(length - 1))
dashline=""
result= str(sum).rjust(length)
for dashes in range(length):
dashline += "-"
if problem != problems[-1]:
first= top + ' '
second = bottom + ' '
dashlines= dashline + ' '
results= result + ' '
else:
first= top
second = bottom
dashlines = dashline
results= result
if show_answers:
realproblem= first + '\n' + second + '\n' + dashlines + '\n' + results
else:
realproblem = first + '\n' + second + '\n' + dashlines
return realproblem
else:
return "Error: Operator must be '+' or '-'."
else:
return 'Error: Numbers cannot be more than four digits.'
else:
return "Error: Numbers must only contain digits."
print(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]))```
if you return from inside this loop, the other problems are not touched. So don’t return the problems here (the errors are fine as those should interrupt the flow anyway and the problems should not be arranged in that case)