I’m trying to complete this project with no success.
i would like to run the first function ONLY if the second argument of the first function is set to true.
after hours non it with no success I’m turning to your for help.
any suggestion is appreciated.
thanks
regards
Andrea
Your code so far
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5: #check if there are more than 5 problems
return 'Error: too many problems'
if any(op in problem for op in ['*','/'] for problem in problems): # division and multiplication error
return "Error: Operator must be '+' or '-'."
arranged_problems = []
first_line = ""
second_line = ""
dashes_line = ""
for problem in problems:
num1, operator, num2 = problem.split()
if not num1.isnumeric() or not num2.isnumeric():
return "Error: Numbers must only contain digits."
if len(num1) > 4 or len(num2) > 4:
return "Error: Numbers cannot be more than four digits."
# splitting the problem into num1, operator, num2
parts = problem.split()
num1 = parts[0]
operator = parts[1]
num2 = parts[2]
# comparing the length of num1 and num2 pick the longest
longest = max(len(num1), len(num2))
# defining the length of the dashes line
dashes = longest + 2
# defining the 3 lines of the arranged problem
problems = ["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]
first_line = ""
second_line = ""
dashes_line = ""
for problem in problems:
# splitting the problem into num1, operator, num2
parts = problem.split()
num1 = parts[0]
operator = parts[1]
num2 = parts[2]
# comparing the length of num1 and num2 pick the longest
longest = max(len(num1), len(num2))
dashes = longest + 2
first_line += num1.rjust(dashes) + " "
second_line += operator + " " + num2.rjust(longest) + " "
dashes_line += "-" * (dashes + 1) + " "
result = first_line.rstrip() + "\n" + second_line.rstrip() + "\n" + dashes_line.rstrip()
if show_answers:
result += "\n" + " answers"
return result
def show_only_if_true():
if show_answers:
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], show_answers))
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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
It looks like you want to call this function when arithmetic_arranger() is called with show_answers=True
Lastly, the show_answers variable is only accessible in the arithmetic_arranger() function, it’s local to that function. It’s not accessible from other functions.
hello there
as for your first question: i was referring to the first function “def arithmetic_arranger(problems, show_answers=False):”
as for the second question
yes i understood that is not possible to access the value outside the first function.
i was wandering if i could do that on the first one or if jut not possible at all?
problems = ["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]
def arithmetic_arranger(problems, show_answers=False):
if len(problems) > 5:
# check if there are more than 5 problems
return 'Error: Too many problems.'
if any(op in problem for op in ['*', '/'] for problem in problems):
# division and multiplication error
return "Error: Operator must be '+' or '-'."
for problem in problems:
num1, operator, num2 = problem.split()
if not num1.isnumeric() or not num2.isnumeric(): # operator should only contain number errors message
return 'Error: Numbers must only contain digits.'
if len(num1) > 4 or len(num2) > 4:
return 'Error: Numbers cannot be more than four digits.'
# defining the 4 lines of the arranged problem
first_line = ""
second_line = ""
dashes_line = ""
result_line = ""
for problem in problems:
# splitting the problem into num1, operator, num2
parts = problem.split()
num1 = parts[0]
operator = parts[1]
num2 = parts[2]
# comparing the length of num1 and num2 pick the longest
longest = max(len(num1), len(num2))
dashes = longest + 2 # defining dash line
# Formatting the 3 lines of the arranged problem
first_line += num1.rjust(dashes) + " "
second_line += operator + " " + num2.rjust(longest) + " "
dashes_line += "-" * (dashes) + " "
result_line += str(eval(num1 + operator + num2)).rjust(dashes) + " "
result = first_line.rstrip() + "\n" + second_line.rstrip() + "\n" + dashes_line.rstrip()+ "\n" + result_line.rstrip() # result line
return result
arithmetic_arranger(["32 + 8", "1 - 3801", "9999 + 9999", "523 - 49"], True)
print(f'\n{arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "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.