Newbie trying to follow the rules to write the codes but it does not seem to work now? Checked indentation, it should be correct could anyone please help? T_T
Your code so far
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
print('Error: Too many problems.')
for problem in problems:
operand1, operand2, operator = problem.split()
if operator != '+' and operator!= '-':
return "Error: Operator must be '+' or '-'."
elif not operand1.isdigit() or not operand2.isdigit():
print('Error: Numbers must only contain digits.')
elif len(operand1) > 4 or len(operand2) > 4:
print('Error: Numbers cannot be more than four digits.')
top = []
middle = []
bottom = []
answers = []
max_len = max(len(operand1), len(operand2)) + 2
top.append(operand1.rjust(max_len))
middle.append(operator + ' ' + operand2.rjust(max_len))
bottom.append('-' * max_len)
if show_answers:
if operator == '+':
results = int(operand1) + int(operand2)
else:
results = int(operand1) - int(operand2)
answers.append(results.rjust(max_len))
else:
answers.append(' ')
return arithmetic_arranger(["3801 - 2", "123 + 49"])
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Challenge Information:
Build an Arithmetic Formatter Project - Build an Arithmetic Formatter Project
when length is longer than 5, print error and then check all the conditions. when length is 1-5, do the math. hmm does it mean i should add an else clause to conclude the if else condition?
Okay I am trying very hard to read the code and understand the issue. Apology if I cannot realize that fast. So in the if statement I misput a bunch of restrictions that i should have checked on those length are not greater than 5 (which is 1-4), does it mean I should move the whole for loop out of if statement?
I tried to move the answer part out of all the restriction loop and if condition. There might be something still wrong nothing showing at console
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
print('Error: Too many problems.')
for problem in problems:
operand1, operand2, operator = problem.split()
if operator != '+' and operator!= '-':
return "Error: Operator must be '+' or '-'."
elif not operand1.isdigit() or not operand2.isdigit():
print('Error: Numbers must only contain digits.')
elif len(operand1) > 4 or len(operand2) > 4:
print('Error: Numbers cannot be more than four digits.')
top = []
middle = []
bottom = []
answers = []
max_len = max(len(operand1), len(operand2)) + 2
top.append(operand1.rjust(max_len))
middle.append(operator + ' ' + operand2.rjust(max_len))
bottom.append('-' * max_len)
if show_answers:
if operator == '+':
results = int(operand1) + int(operand2)
else:
results = int(operand1) - int(operand2)
answers.append(results.rjust(max_len))
else:
answers.append(' ')
return 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.
I have tried to move print function and add the final result, now console is showing File “main.py”, line 45
SyntaxError: ‘return’ outside function no matter how I indent it.
If I comment the return final_result first it will show File “main.py”, line 24, in
NameError: name ‘operand1’ is not defined. Hmm seems like something is wrong up there as well
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
return('Error: Too many problems.')
for problem in problems:
operand1, operand2, operator = problem.split()
if operator not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
elif not operand1.isdigit() or not operand2.isdigit():
return('Error: Numbers must only contain digits.')
elif len(operand1) > 4 or len(operand2) > 4:
return('Error: Numbers cannot be more than four digits.')
top = []
middle = []
bottom = []
answers = []
max_len = max(len(operand1), len(operand2)) + 2
top.append(operand1.rjust(max_len))
middle.append(operator + ' ' + operand2.rjust(max_len))
bottom.append('-' * max_len)
if show_answers:
if operator == '+':
results = int(operand1) + int(operand2)
else:
results = int(operand1) - int(operand2)
answers.append(results.rjust(max_len))
else:
answers.append(' ')
final_result = ' '.join(top) + '\n' + ' '.join(middle) + '\n' + ' '.join(bottom)
if show_answers:
final_result += '\n' + ' '.join(answers)
return final_result
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')
I see. Now I move function code inside function and call it from outside. Console only showing Error: Operator must be ‘+’ or ‘-’. It seems like it got stuck here even though there is only + - when I call function?
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
return('Error: Too many problems.')
for problem in problems:
operand1, operand2, operator = problem.split()
if operator not in ['+', '-']:
return "Error: Operator must be '+' or '-'."
elif not operand1.isdigit() or not operand2.isdigit():
return('Error: Numbers must only contain digits.')
elif len(operand1) > 4 or len(operand2) > 4:
return('Error: Numbers cannot be more than four digits.')
top = []
middle = []
bottom = []
answers = []
max_len = max(len(operand1), len(operand2)) + 2
top.append(operand1.rjust(max_len))
middle.append(operator + ' ' + operand2.rjust(max_len))
bottom.append('-' * max_len)
if show_answers:
if operator == '+':
results = int(operand1) + int(operand2)
else:
results = int(operand1) - int(operand2)
answers.append(results.rjust(max_len))
else:
answers.append(' ')
final_result = ' '.join(top) + '\n' + ' '.join(middle) + '\n' + ' '.join(bottom)
if show_answers:
final_result += '\n' + ' '.join(answers)
return final_result
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])}')