My code isnt passing any of the tests an I dont know why when i ran my own tests everything was working with t he example problems given to us in the beginning please help me I do not understand
Your code so far
def arithmetic_arranger(problems, show_answers=False):
top = []
middle = []
dashes = []
answer_row = []
answer = ''
if len(problems)>5:
return 'Error: Too many problems'
for problem in problems:
oper = 0
op = ''
i = 0
t1 = False
t2 = False
while i<len(problem) and t1 == False:
if problem[i] =='+' or problem[i] == '-':
t1 = True
oper = i
op = problem[i]
i+=1
if t1 == True:
parts = problem.split()
part1 = parts[0]
part2 = parts[2]
if part1.isdigit()==False or part2.isdigit() == False:
return 'Error: Numbers must only contain digits.'
if t1 == False:
return "Error: Operator must be '+' or '-'"
if (len(part1) or len(part2))>4:
return 'Error numbers cannot be more than four digits'
if show_answers == True and problem[oper]=='+':
answer = str(int(part1)+int(part2))
elif show_answers == True and problem[oper]=='-':
answer = str(int(part1)-int(part2))
max_length = max(len(part1),len(part2))+2
answer_row.append(answer.rjust(max_length))
answer_row.append('')
top.append(part1.rjust(max_length))
top.append('')
middle.append(f'{op}{" "*((max_length-1)-len(part2)-1)} {part2}'.ljust(max_length))
middle.append('')
dashes.append('-'*max_length)
dashes.append('')
solved = ' '.join(top) + '\n' + ' '.join(middle) + '\n' + ' '.join(dashes)
if show_answers == True:
solved += '\n' + ' '.join(answer_row)
return solved
print(arithmetic_arranger(["3801 - 2", "123 - 49"]))
print(arithmetic_arranger(["1 + 2", "1 - 9380"],))
print(arithmetic_arranger(["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]))
print(arithmetic_arranger(["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]))
print(arithmetic_arranger(["3 + 855", "988 + 40"], True))
print(arithmetic_arranger(["32 - 698", "1 - 3801", "45 + 43", "123 + 49", "988 + 40"], True))
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3.1 Safari/605.1.15
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project
The first one is when i did repr()
and the second is after is what i get for my output without the repr() function for these two arrays:
[“3801 - 2”,”123 - 49”] and [“1 + 2”,”1 - 9380”]
the lines here are the dashes by the way for some reason it looks like that on the forum page please see the screenshot as that what it actually looks like I don’t know why it looks like this here.
I’ve edited your post to improve the readability of the code. 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.
if you print the strings in the tests you will see that they appear as the example
here I am printing the output from your function and after that the test string, your output has too many spaces
the example always have only 4 spaces between problems, it’s written under “If the user supplied the correct format of problems, the conversion you return will follow these rules:”
THIS IS MY CODE>
I changed it so it matches the test case but the code still doesnt pass the tests, I did it how you wanted using the print function to compare the tests and it still does not work please help me
def arithmetic_arranger(problems, show_answers=False):
top = []
middle = []
dashes = []
answer_row = []
answer = ''
if len(problems)>5:
return 'Error: Too many problems.'
for problem in problems:
oper = 0
op = ''
i = 0
t1 = False
t2 = False
while i<len(problem) and t1 == False:
if problem[i] =='+' or problem[i] == '-':
t1 = True
oper = i
op = problem[i]
i+=1
if t1 == True:
parts = problem.split()
part1 = parts[0]
part2 = parts[2]
if part1.isdigit()==False or part2.isdigit() == False:
return 'Error: Numbers must only contain digits.'
if t1 == False:
return "Error: Operator must be '+' or '-'."
if (len(part1))>4 or len(part2)>4:
return 'Error: Numbers cannot be more than four digits.'
if show_answers == True and problem[oper]=='+':
answer = str(int(part1)+int(part2))
elif show_answers == True and problem[oper]=='-':
answer = str(int(part1)-int(part2))
max_length = max(len(part1),len(part2))+2
answer_row.append(answer.rjust(max_length))
answer_row.append('')
top.append(part1.rjust(max_length))
top.append('')
middle.append(f'{op}{" "*((max_length-1)-len(part2)-1)} {part2}'.ljust(max_length))
middle.append('')
dashes.append('-'*max_length)
dashes.append('')
solved = ' '.join(top) + '\n' + ' '.join(middle) + '\n' + ' '.join(dashes)
if show_answers == True:
solved += '\n' + ' '.join(answer_row)
return solved
honestly thank you so much since I am quite new I didn’t know how to properly compare results with tests and ur help made it so uhh easier thanks so much it works now