Python Projects - Arithmetic Formatter HELP

Hi all, I’m having trouble with the Arithmetic Formatter Python Project. Here is my code:

def arithmetic_arranger(problems, answer=False):
  first_int = problems
  operator = '+' or '-'
  second_int = problems + first_int
  total_problems = first_int, operator, second_int

  if len(total_problems) > 5:
    print("Error: Too many problems.")
  elif operator != '+' or operator != '-':
    print("Error: Operator must be '+' or '-'.")
  elif first_int or second_int != int:
    print("Error: Numbers must only contain digits.")
  elif first_int or second_int > 4:
    print("Error: Numbers cannot be more than four digits.")
  else:
    single_space = first_int, operator, second_int
    print(single_space)
    right_align = first_int.rjust, second_int.rjust
    print(right_align)
    four_spaces = total_problems + '    ' + '----'
    print(four_spaces)
    
    return arranged_problems

print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True))```

Welcome to the forum.

Can you please explain a bit more about how your code isn’t doing what you expect?

1 Like

I suspect that these conditions are your issue. You need to have elif (condition1) or (condition2):, where condition1 and condition2 are full logical conditions.

1 Like

Unfortunately all 6 of my tests are failing, so I believe it goes beyond just those 2 elif statements

What does your code look like with those conditionals fixed?

Like this?

def arithmetic_arranger(problems, answer=False):
  first_int = problems
  operator = '+' or '-'
  second_int = problems + first_int
  total_problems = first_int, operator, second_int

  if len(total_problems) > 5:
    print("Error: Too many problems.")
  elif (operator != '+' or operator != '-'):
    print("Error: Operator must be '+' or '-'.")
  elif (first_int != int or second_int != int):
    print("Error: Numbers must only contain digits.")
  elif (first_int > 4 or second_int > 4):
    print("Error: Numbers cannot be more than four digits.")
  else:
    single_space = first_int, operator, second_int
    print(single_space)
    right_align = first_int.rjust, second_int.rjust
    print(right_align)
    four_spaces = total_problems + '    ' + '----'
    print(four_spaces)
    
    return arranged_problems

print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True))```

In this statement you are setting operator to the result of or between two strings (which in this case returns a ‘+’?)

In this test, you check if the operator is either not - or +, but this will always return true because operator cannot be both.

This test doesn’t check if you have more than 4 digits. This checks if your value is bigger than 4.

I also don’t think you are breaking apart your array of strings in the way you think that you are?

Is this any better? Still not sure what I’m doing wrong:

def arithmetic_arranger(problems, answer=False):
  num = [" "]
  operator = ["+", "-"]
  den = [" "]
  total = num + operator + den

  if len(total) > 5:
    return Error: Too many problems.
  elif (operator == "*") or (operator == "/"):
    return Error: Operator must be '+' or '-'.
  elif (num != int) and (den != int):
    return Error: Numbers must only contain digits.
  elif len(num) > 4 or len(den) > 4:
    return Error: Numbers cannot be more than four digits.
  else:
    total.str(" ")
    num.rjust, den.rjust
    total.str("    ")
    total.str(----)
  return arranged_problems

print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True))

I think you will need quotation marks in your return lines like

    return "Error: Too many problems."

Still failing 4 tests:

def arithmetic_arranger(problems, answer=False):
  if len(problems) > 5:
    return "Error: Too many problems."

  elif (problems) == "*" or (problems) == "/":
    return "Error: Operator must be '+' or '-'."

  elif (problems) != int:
    return "Error: Numbers must only contain digits."

  elif len(problems) > 4:
    return "Error: Numbers cannot be more than four digits."

  else:
    problems.str(" ")
    problems.rjust
    problems.str("    ")
    problems.str("----")
    
  return arranged_problems

print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True))

Hello there,

These are not doing what you think:

elif (problems) != int:
    return "Error: Numbers must only contain digits."
elif (problems) == "*" or (problems) == "/":
    return "Error: Operator must be '+' or '-'."
elif len(problems) > 4:
    return "Error: Numbers cannot be more than four digits."

Actually, there is very little happening in your code…

Your first statement:

if len(problems) > 5:

This is correct.

Now, for the rest, I suggest you delete all of the rest of the function code, and add this: print(problems)

From there, you will be able to see:

  1. When would problems ever be equal to "*" or "/"?
  2. What does (problems) != int mean?
  3. If len(problems) > 5 makes sense, as it tells you the number of problems, hows does len(problems) > 4 make sense, if it also checks the number of digits?

Finally, you are returning arranged_problems, but have not defined what that is…

Spending a few minutes more on the material linked on the project page would be helpful: https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/arithmetic-formatter

Hope this helps

2 Likes

Helpful, thank you! I switched to nested loops, but still not passing all tests:

def arithmetic_arranger(problems, answer=False):
  if len(problems) > 5:
    return "Error: Too many problems."
  for i in problems:
      if i == '*' or '/':
          return "Error: Operator must be '+' or '-'."
  for j in problems(len - 1):
      if j != int:
          return "Error: Numbers must only contain digits."
      elif len(j) > 4:
          return "Error: Numbers cannot be more than four digits."

  arranged_problems = problems[i] + "\n" + problems[i, j].rjust + problems[i, j].str("    ") + problems[j][:-4]
  return arranged_problems

print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"], True))

Can you not see any errors in the console? You have not defined a whole heap of variables you are using.

This is not doing what you expect: if i == '*' or '/':
len does not exist: for j in problems(len - 1):
int is a class declaration variable not something to compare against: if j != int:
i and j are not defined: problems[i] + "\n" + problems[i, j].rjust + problems[i, j].str(" ") + problems[j][:-4]

These are basic issues that should be brought up in the console, when you run the script. It is a very useful skill getting to know the console, and understanding its output.

Hope this helps

Did you manage to get the solution?