I’ve tried applying all the tests to what I’ve coded but they’re all still displaying “x” despite it having worked for all the tests. I don’t know what else to do.
Your code so far
def arithmetic_arranger(problems, give_answer=False):
#Assign problems into a variable
operations = problems[0]
#print(problems[0].isnumeric())
#print(operations)
#Setup strings per line
arranged_problems_1 = ""
arranged_problems_2 = ""
arranged_problems_3 = ""
arranged_problems_4 = ""
#Return an error if there are * or / operators
for problem in problems:
if ("*" in problem or "/" in problem):
print("Error: Operator must be '+' or '-'.")
return
#Return an error if there are more than 5 problems
if len(problems) > 5:
print("Error: Too many problems.")
return
#Split each problem into 3 elements; first element for arranged_1, and second and third elements for arranged_2
for i in range(len(problems)):
problems[i] = problems[i].split(" ")
#print(problems[i])
#For-loop to check some conditions that will print errors
for i in range(len(problems)):
#Print an error message if any characters within operons aren't numeric
if problems[i][0].isnumeric() == 0 or problems[i][2].isnumeric() ==0:
print("Error: Numbers must only contain digits.")
return
#Print an error message if any of the operons are more than 4 characters long
if len(problems[i][0]) > 4 or len(problems[i][2]) > 4:
print("Error: Numbers cannot be more than four digits.")
return
#Print operations
#Calculating length of largest operon for each problem
largest_list = []
for i in range(len(problems)):
if len(problems[i][0]) > len(problems[i][2]):
largest_list.append((len(problems[i][0]), 1))
else:
largest_list.append((len(problems[i][2]), 2))
#print(largest_list)
#First line
#largest_list type list of tuple(int,int)
#problems type list of list of 'str'
for i in range(len(problems)):
#print(largest_list[i][0])
#print(len(problems[i][0]))
for size in range(largest_list[i][0] + 2 - len(problems[i][0])):
arranged_problems_1 += ' '
arranged_problems_1 += (problems[i][0])
if (len(arranged_problems_1)) < 40:
arranged_problems_1 += ' '
#arranged_problems_1 += '\n'
#Second line
for i in range(len(problems)):
arranged_problems_2 += problems[i][1] + ' '
for size in range(largest_list[i][0] - len(problems[i][2])):
arranged_problems_2 += ' '
arranged_problems_2 += problems[i][2]
if (len(arranged_problems_2)) < 40:
arranged_problems_2 += ' '
#arranged_problems_2 += '\n'
#Dashes
for i in range(len(problems)):
for i in range(largest_list[i][0] + 2):
arranged_problems_3 += '-'
if (len(arranged_problems_3)) < 40:
arranged_problems_3 += ' '
#Print the answer if 2nd arguement is "True"
if give_answer == True:
#arranged_problems_4 += '\n'
for i in range(len(problems)):
#Addition
if problems[i][1] == "+":
answer = int(problems[i][0]) + int(problems[i][2])
#Subtraction
elif problems[i][1] == "-":
answer = int(problems[i][0]) - int(problems[i][2])
#Print if incorrect operator
else:
print(f"{problems[i][1]} isn't an accepted operator.")
return
#Print answer
for size in range(largest_list[i][0] + 2 - len(str(answer))):
arranged_problems_4 += ' '
arranged_problems_4 += str(answer)
if (len(arranged_problems_4)) < 40:
arranged_problems_4 += ' '
#print(largest_list)
print(arranged_problems_1)
print(arranged_problems_2)
print(arranged_problems_3)
print(arranged_problems_4)
#print("\n")
arithmetic_arranger(["4111 + 8151", "9019 - 2222", "4511 + 2224", "1213 + 2224"], True)
arithmetic_arranger(["3 / 855", "3801 - 2", "45 + 43", "123 + 49"])
arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)
arithmetic_arranger(["3801 - 2", "123 + 49"], True)
#
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project
I changed part of the code to better see where you are adding spaces. seems like there is trailing spaces to pass the tests you will need to account for the last problem and not add any extra space.
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.
Hello! So I rewrote parts of it and used removesuffix to remove the trailing spaces, but it still won’t read it as correct. I honestly have no idea what I’m doing wrong.
def arithmetic_arranger(problems, give_answer=False):
#Assign problems into a variable
#print(problems[0].isnumeric())
#print(operations)
#Setup strings per line
arranged_problems = ""
#Return an error if there are * or / operators
for problem in problems:
if ("*" in problem or "/" in problem):
print("Error: Operator must be '+' or '-'.")
return
#Return an error if there are more than 5 problems
if len(problems) > 5:
print("Error: Too many problems.")
return
#Split each problem into 3 elements; first element for arranged_1, and second and third elements for arranged_2
for i in range(len(problems)):
problems[i] = problems[i].split(" ")
#print(problems[i])
#For-loop to check some conditions that will print errors
for i in range(len(problems)):
#Print an error message if any characters within operons aren't numeric
if problems[i][0].isnumeric() == 0 or problems[i][2].isnumeric() ==0:
print("Error: Numbers must only contain digits.")
return
#Print an error message if any of the operons are more than 4 characters long
if len(problems[i][0]) > 4 or len(problems[i][2]) > 4:
print("Error: Numbers cannot be more than four digits.")
return
#Print operations
#Calculating length of largest operon for each problem
#List of longer operons
largest_list = []
for i in range(len(problems)):
if len(problems[i][0]) > len(problems[i][2]):
largest_list.append((len(problems[i][0]), 1))
else:
largest_list.append((len(problems[i][2]), 2))
#print(largest_list)
#List of shorter operons
smallest_list = []
for i in range(len(problems)):
if len(problems[i][0]) < len(problems[i][2]):
smallest_list.append((len(problems[i][0]), 1))
else:
smallest_list.append((len(problems[i][2]), 2))
#print(smallest_list)
#First line
top_string = ''
for i in range(len(problems)):
addspaces = len(problems[i][2]) - len(problems[i][0])
if len(problems[i][0]) >= len(problems[i][2]):
top_string += ' ' + (problems[i][0])
else:
for x in range(addspaces):
top_string += ' '
top_string += ' ' + (problems[i][0])
top_string += ' '
stripped = top_string.removesuffix(' ')
stripped += '\n'
#Second line
for i in range(len(problems)):
if len(problems[i][0]) <= len(problems[i][2]):
stripped += (problems[i][1]) + ' ' + (problems[i][2])
else:
stripped += (problems[i][1]) + ' '
for length in range(largest_list[i][0] - smallest_list[i][0]):
stripped += ' '
stripped += (problems[i][2])
stripped += ' '
second_stripped = stripped.removesuffix(' ')
second_stripped += '\n'
#Dashes
for i in range(len(problems)):
for i in range(largest_list[i][0] + 2):
second_stripped += '-'
second_stripped += ' '
no_answer = second_stripped.removesuffix(' ')
#Print the answer if 2nd arguement is "True"
if give_answer == True:
no_answer += '\n'
for i in range(len(problems)):
#Addition
if problems[i][1] == "+":
answer = int(problems[i][0]) + int(problems[i][2])
#Subtraction
else:
answer = int(problems[i][0]) - int(problems[i][2])
for i in range(largest_list[i][0] + 2 - (len(str(answer)))):
no_answer += ' '
no_answer += str(answer)
no_answer += ' '
final_answer = no_answer.removesuffix(' ')
print(final_answer)
else:
print(no_answer)
arithmetic_arranger(["9999 + 9999", "123 + 49"], True)
arithmetic_arranger(["1 + 2", "1 - 9380"], True)
arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True)
I actually was just able to get it to pass, just now!! Thank you for everyone’s help. Apparently I reverted everything back to print() because I was trying to see what it looked like when I should have been using return(). Thank you to everyone again!