Hi! Im struggling with this challenge for a long time. I watch tutorials, forum post, etc, etc for guiding me but even with all of that i cant figure out what is the problem with my code. In top of that, i cant read correctly the console for see what are the error that are throwing at me.
I will be so glad if you can help me because i am stuck and i need my certification : c
Thank you!
Your code so far
def arithmetic_arranger(problems, solver = False):
#Principal Variables
first_number = ""
operator = ""
second_number = ""
lines = ""
solution = ""
#First Check after the FOR loop
if len(problems) > 5:
return "Error: Too many problems."
#Core FOR Loop
for problem in problems:
problem_array = problem.split()
first_number = problem_array[0]
operator = problem_array[1]
second_number = problem_array[2]
#Checks for the items before the split
if first_number.isdigit() and second_number.isdigit():
if len(first_number) > 4 < len(second_number):
return "Error: Numbers cannot be more than four digits"
else:
return "Error: Numbers must only contain digits."
if operator == "+":
solution == int(first_number) + int(second_number)
elif operator == "-":
solution == int(first_number) - int(second_number)
else:
return "Error: Operator must be '+' or '-'."
#Distance comprobation
distance = max(len(first_number), len(second_number)) + 2
#Items Padding
top_item = str(first_number.rjust(distance)) + " "
bottom_item = str(operator.ljust(distance)) + " " +str(second_number.rjust(distance)) + " "
lines = ""
solution_item = str(solution.rjust(distance))
for line in range(distance):
lines += "-"
if solver == True:
operations = top_item + "/n" + bottom_item + "/n" + lines + "/n" + solution_item
else:
operations = top_item + "/n" + bottom_item + "/n" + lines
return operations
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36
Here we got the error message and the error formatted.
The green line is your code and if we look closely at it, at the expected red output AND the arguments, it seems like you are showing the calculations in reverse order.
When you are looping through the âtop lineâ , âbottom lineâ and all of that, it would be best if you store each of them in a variable when looping or else, the âforâ loop is just gonna give you the last value of the iterationâŚ
Hereâs what I mean
a = ['a' ,'b' , 'c']
for alphabet in a:
answer = alphabet
If you print your answer like this:
a = ['a' , 'b' , 'c']
for alphabet in a:
answer = alphabet
print(answer)
Itâs just going to give you âcâ as the final answer
Because for the first iteration, the alphabet is âaâ (without us doing something to it)
for the second iteration, the alphabet is âbâ (without us doing something to it)
and for the third iteration, the alphabet is âcââŚwhich is what youâll finally print out
So, iâll suggest you do something within the code like this
a = ['a' , 'b' , 'c']
output = ''
for alphabet in a:
#Do something to the code
word = alphabet + 'y'
output = output + word
print(output)
The final answer should look like this âaybycyâ
Try to make this adjustment to your code first or else it will carry out the last problem in all the problems of your code
Mmm, so, How i can do the padding in that way? Because for that i nedd like im doing, separate every number in a variable(first_number, operator, etc) if i want positioning them right.
If i make an array for store the operations i cant do the paggind like this:
Try putting everything in one for loop and then create a general list where you can append all your answers
Hereâs what I mean:
def arithmetic_arranger(problems, solver = False):
#Principal Variables
final_answers = []
first_number = ""
operator = ""
second_number = ""
lines = ""
solution = ""
#First Check after the FOR loop
if len(problems) > 5:
return "Error: Too many problems."
#Core FOR Loop
for problem in problems:
problem_array = problem.split()
first_number = problem_array[0]
operator = problem_array[1]
second_number = problem_array[2]
#Checks for the items before the split
if first_number.isdigit() and second_number.isdigit():
if len(first_number) > 4 < len(second_number):
return "Error: Numbers cannot be more than four digits"
else:
return "Error: Numbers must only contain digits."
if operator == "+":
solution == int(first_number) + int(second_number)
elif operator == "-":
solution == int(first_number) - int(second_number)
else:
return "Error: Operator must be '+' or '-'."
#Distance comprobation
distance = max(len(first_number), len(second_number)) + 2
#Items Padding
top_item = str(first_number.rjust(distance))
bottom_item = str(operator.ljust(distance)) + " " +str(second_number.rjust(distance))
lines = ""
solution_item = str(solution.rjust(distance))
for line in range(distance):
lines += "-"
if solver == True:
operations = top_item + "\n" + bottom_item + "\n" + lines + "\n" + solution_item
final_answers.append(operations)
else:
operations = top_item + "\n" + bottom_item + "\n" + lines
final_answers.append(operations)
return ' '.join(final_answers)
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True))
Though this code code wonât work perfectly , its just to let you know what you have to include and give you an idea alsoâŚ
Note that i created a list with the variable name âfinal_answerâ
I also added other preceeding codes under the âfor loopâ and not outside of it
I also appended each âoperationsâ to âfinal_answerâ after each iterations
You should also note that what youâre finally going to return should be outside of the for loop or else youâll only get the first problem solving instead of everything
Hmmm, for that, we can do it this way instead of using final_answers listâŚ
Try taking all the top_lines in one list (i.e top = [ ])
then bottom_lines in another list (example bottom = [ ])
and so on...
So, for each iteration try appending your top_lines to the top list
E.g top.append(top_lines)
So, you can then finally return (' '.join(top) + '\n' +' '.join(operator) + ' '.join(bottom) +'\n' + ' '.join(solution)
If you still have questions, then just write about it and probably someone will do justice to it
Where i am here, itâs exactly 2:01 am and its way past my bed time
I have to go to bed now to prepare for a busy day tomorrow(lol ,well, today actually)âŚ
Iâll see you laterâŚbye
Thanks, but i already tried that, i think its a thing in the code i cant see, a little detail like some identation, i dont know. Because i see other codes for this challenge where even without the use of lists the operations are printed out correctly in a single line, and are very simmilar to my first code posted hereâŚ