I'm stuck with the : Arithmetic Formatter

Tell us what’s happening:
The 1st part got okay-ish but then the last requirement i need to fulfil are quite a challenge
Like I cannot seem to find out how to add a dash to it or how to add the 4 spaces and the dashes together.
Could use some help here.
Your code so far

def arithmetic_arranger(problems):
  x = input("Enter a number")
  y = input("Enter a + or -")
  z = input("Enter another number")
  

  if(problems > 6)  #exceeds the maxinum problem of 5 assigned to the function
  print( "Error: Too many problems.")
  elif(y !== + OR -)#anything else other then a + or - is used by user
  print("Error: Operator must be '+' or '-'")
  elif(y != int OR x != int)#only numbers
  print("Error: Numbers must only contain digits.")
  elif(x OR z > 5)#Each operand (aka number on each side of the operator) has a max of four digits in width. Otherwise, the error string returned will be:
  print("Error: Numbers cannot be more than four digits.")
  else()
  print(x /n,  y /n, z /n)
  #Numbers should be right-aligned. = f"{number:>width}"
  #4 spaces /n
  #dashes i can't find those google return me to a program Dash
    return arranged_problems



Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36.

Challenge: Declare JavaScript Variables

Link to the challenge:

2 Likes

you are not indenting the code, in python indentation is of upmost importance

1 Like

wrong:

def task(problem):
    if(condition OR + OR -)
    print(text)
    else
    pass
        return solution # variable was not defined

correct:

def task(problem):    
    solution = ""

    if(condition or "+" or "-"):
        print(text)
    else:
        pass

    return solution
        
1 Like

Okay i changed that one

def arithmetic_arranger(problems):
    #Something wrong here i'm misunderstanding something what must be inputed
    x = input("Enter a number")
    y = input("Enter a + or -")
    z = input("Enter another number")
  

    if(problems > 6)  #exceeds the maxinum problem of 5 assigned to the function
        print( "Error: Too many problems.")
    elif(y !== + OR -)#anything else other then a + or - is used by user
        print("Error: Operator must be '+' or '-'")
    elif(y != int OR x != int)#only numbers
        print("Error: Numbers must only contain digits.")
    elif(x OR z > 5)#Each operand (aka number on each side of the operator) has a max of four digits in width. Otherwise, the error string returned will be:
        print("Error: Numbers cannot be more than four digits.")
    else()
    b = x, y ,z
    a = b.split()
        print(a)
    return arranged_problems

Personal log

#the full task https://repl.it/@freeCodeCamp/fcc-arithmetic-arranger#README.md

def arithmetic_arranger(problems):
    l = len(problems)
    for p in problems: 
        x,y,z = p.split()
        x = int(x)
        z = int(z)
    if(l > 5) : #exceeds the maxinum problem of 5 assigned to the function
        print( "Error: Too many problems.")
    elif y != '+' or y != '-':#anything else other then a + or - is used by user
        print("Error: Operator must be '+' or '-'")
    elif(type(x) != int or type(z) != int):#only numbers
        print("Error: Numbers must only contain digits.")
    elif(x or z > 5):#Each operand (aka number on each side of the operator) has a max of four digits in width. Otherwise, the error string returned will be:
        print("Error: Numbers cannot be more than four digits.")
    else:
        pass
    return arranged_problems
    
    
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"]))

i see your code says > 6 i think it should be >5

1 Like

Thanks I will change it

It is changed anything else?

def arithmetic_arranger(problems):
    l = len(problems)
    for p in problems: 
        x,y,z = p.split()
        x = int(x)
        z = int(z)
    if(l > 6) : #exceeds the maxinum problem of 5 assigned to the function
        print( "Error: Too many problems.")
    elif y != '+' or y != '-':#anything else other then a + or - is used by user
        print("Error: Operator must be '+' or '-'")
    elif(type(x) != int or type(z) != int):#only numbers
        print("Error: Numbers must only contain digits.")
    elif(x or z > 6):#Each operand (aka number on each side of the operator) has a max of four digits in width. Otherwise, the error string returned will be:
        print("Error: Numbers cannot be more than four digits.")
    else:
        pass
    return arranged_problems
    
    
print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123

You shoud write if l >= 6. Ir means ir doesn’t allow 6 ir more

1 Like

if you write “-” * 4 you will get 4 dashes ----

1 Like

each condition should be complete on its own, x is not a complete condition

2 Likes

I have no idea like what the result would be or what im suppose to do, how im suppose to be arranged

Repl.it - fcc-arithmetic-arranger
read that one

Why are you writing in such a crude way;
Use try-except block for error;
they are more robust than if-else.

I don’t know why this causes error although it works perfectly fine.

def arithmetic_arranger(problems, answer=False):
  if len(problems) > 5:
    return "Error: Too many problems."
  operator = ("+", "-")
  first_line = ""
  second_line = ""
  third_line = ""
  fourth_line = ""
  for operation in problems:
    op_split = operation.split()
    if op_split[1] in operator:
      if not op_split[0].isdigit() or not op_split[2].isdigit(): return "Error: Numbers must only contain digits."
      if len(op_split[0]) > 4 or len(op_split[2]) > 4: return "Error: Numbers cannot be more than four digits."
      greater = len(op_split[0]) if len(op_split[0]) > len(op_split[2]) else len(op_split[2])
      first_line += " " * (greater - len(op_split[0]) + 2) + op_split[0] + " "*4
      second_line += op_split[1] + " " * (greater - len(op_split[2]) + 1) + op_split[2] + " "*4
      for i in range(greater + 2) : third_line += "-"
      third_line += " "*4
      if answer:
        ans = str(eval(operation))
        fourth_line += " "*(greater + 2 - len(ans)) + ans + " " * 4
    else:
      return "Error: Operator must be '+' or '-'."

  if answer:
    return first_line + "\n" +second_line+ "\n" + third_line + "\n" + fourth_line
  return first_line + "\n" +second_line+ "\n" + third_line

Just took a brief look, and one issue looks to me like, is the extra white-space after printing the last item.

I believe the maximum number of digits is four so I would use x or z >= 5.

It is easier for others to read your code if you use descriptive variable names, i.e. change x to top_oper, y to operand and z to bottom_oper.