Please help. I don`t understand what's wrong with my code

Tell us what’s happening:
Describe your issue in detail here.
I don’t understand what`s wrong with my code.

Your code so far

def arithmetic_arranger(problems, show=False):
  e = 0
  while e == 0:
    if len(problems) > 5:
      return "Error: Too many problems."
      e = 1
      break
    if show == False:  
      arranged_problems = ["" for _ in range(3)]
    elif show == True:
      arranged_problems = ["" for _ in range(4)]

    for _ in problems: 
      lst = _.split(" ")
      
      try:
        i = int(lst[0]) + int(lst[2])
      except:
        return "Error: Numbers must only contain digits."
        e = 1
        break
      if len(lst[0]) > 4 or len(lst[2]) > 4:
        return "Error: Numbers cannot be more than four digits."
        e = 1
        break
      if len(lst[0]) >= len(lst[2]):
        gensize = len(lst[0]) + 2
      else:
        gensize = len(lst[2]) + 2
      sign = lst[1]
      if sign != "-" and sign != "+":
        return "Error: Operator must be '+' or '-'."
        e = 1
        break
      if show == True:
        for x in range(4):
          string = ""
          
          if x == 0:
            string = " " * (gensize - len(lst[0])) + lst[0]
          elif x == 1:
            string = sign + " " * (gensize - len(lst[2]) - 1) + lst[2]
          elif x == 2:
            string = "-" * gensize
          elif x == 3:
            if sign == "-":
              string = " " * (gensize - len(str(int(lst[0]) - int(lst[2])))) + str(int(lst[0]) - int(lst[2]))
            elif sign == "+":
              string = " " * (gensize - len(str(int(lst[0]) + int(lst[2])))) + str(int(lst[0]) + int(lst[2]))
          arranged_problems[x] += string + " " * 4
        
        
        
            
        
      elif show == False:
        for x in range(3):
          if x == 0:
            string = " " * (gensize - len(lst[0])) + lst[0]
          elif x == 1:
            string = sign + " " * (gensize - len(lst[2]) - 1) + lst[2]
          elif x == 2:
            string = "-" * gensize
          arranged_problems[x] += string + " " * 4
          
        
    a = ""
    for _ in arranged_problems:
      a += _[:-4] + "\n"
    a[:-2]
    
        
    return a
    break 

Your browser information:

User Agent is: Opera (Windows 10.; Win64; x64)

Challenge: Arithmetic Formatter

Link to the challenge:

I’ve edited your post 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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hello!

That’s one of your error messages:

E   AssertionError: Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]
E   assert '  3801      ...--    -----\n' == '  3801      ...----    -----'

Your output has a \n (newline) at the end of your string, but the test expects no \n
Try to fix that!

I noticed some confusion regarding the possibilities to exit a function or ending a loop.
I.E.:

There is nothing wrong with making the e == 0 condition, but if let’s say len(problems) > 5 is True, then:

  1. return "Error: Too many problems." gets executed directly. In particular after whatever gets calculated in the line of the return keyword, it will be the result of the call of the function. That’s why whatever comes after the return line will never be used and you might discard it directly.
  2. If there would be no return in that if clause like here:
while e == 0:
    if len(problems) > 5:
      e = 1
      break

the break in the last line stops your loop. So break is used when you want to quit the loop directly, while you use e = 1 if you still want to execute whatever the loop contains after the redeclaration of e.
There is no need for a specific statement in your while condition if you plan to eventually stop the loop with break, just make sure that the condition for the break is eventually True:

while True:
    some_code
    if condition == True:
        break
1 Like