Arithmetic Formatter return formated string

I don’t understand why my yield statement isn’t working or am not using it correctly if I use print it works isn’t yield supposed to return or do I need to return something else sorry I don’t like to bother people but I was confused as to why I need to return something . Thanks

def arithmetic_arranger(arithm_list, cond=None):

    if len(arithm_list) > 5:

        return 'Error: Too many problems.'

    problem_list = []

    for (i, problem) in enumerate(arithm_list):

        problem_list.append(problem.split())

    for i in problem_list:

        if [j for j in i if len(j) > 4]:

            return 'Error: Numbers cannot be more than four digits.'

        while True:

            try:

                if i[1] == '+':

                    i.append('-----')

                    i.append(int(i[0]) + int(i[2]))

                elif i[1] == '-':

                    i.append('-----')

                    i.append(int(i[0]) - int(i[2]))

                else:

                    return "Error: Operator must be '+' or '-'."

            except ValueError:

                return 'Error: Problems must only contain digits.'

            else:

                break

    if cond == False:

        return

    elif cond == None:

        return

    else:

        pass

    final_list = []

    for (i, characters) in enumerate(problem_list):

        problem = \

            """

{0:>10}

{1:>5}{2:>5}

{3:>10}

{4:>10}

      """.format(characters[0],

                characters[1], characters[2], characters[3],

                characters[4]).split('\n')

        final_list.append(problem)

    for i in zip(*final_list):

        yield '  '.join(i)
        


test program
ints_list = ["32 + 698", "2 - 3801", "45 + 43", "123 + 49"]

print(arithmetic_arranger(ints_list,True))

Your browser information:

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

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 (’).

the tests test the output of your function - you need to output the required values with the return keyword

1 Like

Thanks I got it to work I was not calling the generator.

ints_list = ["32 + 698", "2 - 3801", "45 + 43", "123 + 49"]
for x in arithmetic_arranger(ints_list,True):
    print(x) ```

output
            32                  2            45         123
    +  698      - 3801      +   43      +   49
         -----           -----         -----         -----
         730       -3799           88         172

sorry it looks like this ● arith_format.py - Python_Course_Work - Visual Studio Code 12_8_2020 6_37_20 PM

and what do the tests say?

Sorry sir it failed i don't know how to return so I gave up.  Yield will not work as the return staments did not exit from the generator. However, this way it works oh well I just give coding

def arithmetic_arranger(arithm_list,cond=None): 
   if len(arithm_list) > 5: return "Error: Too many problems." #check len of list 
   problem_list = [] # empty list to append lists 
   for i, problem in enumerate(arithm_list):
     problem_list.append(problem.split())
     #appends list to create nested list. example [['23','+','5']['23','-','5']]
   for i in problem_list:
      if([j for j in i if len(j)>4]): # checks if len of digit sequence is > 4
          return "Error: Numbers cannot be more than four digits."
      while True:
         try:
          if(i[1]=="+"): # checks if the second value in i is either - +
             i.append("-----")    
             i.append(int(i[0])+int(i[2])) # if conditon true,try to parse, sum, append ->i
          elif(i[1]=="-"):
             i.append("-----")    
             i.append(int(i[0])-int(i[2])) # if conditon true,try to parse,subt, append ->i
          else:
            return "Error: Operator must be '+' or '-'."          
         except ValueError: # handles parsing error
             return "Error: Problems must only contain digits."
         else:
             break 
   if(cond==False):
      return 
   elif(cond==None):
      return
   else:
      pass          
   final_list = [] # final list will contain our formated strings
   for i, characters in enumerate(problem_list):
# for loop iterates problem_list to create a list of formated strings using list indexes   
        problem = """
{0:>10}
{1:>5}{2:>5}
{3:>10}
{4:>10}
      """.format(characters[0], characters[1], characters[2],characters[3],characters[4]).split('\n')
        final_list.append(problem)
   for i in zip(*final_list): #finally this allows us to zip the list and print by joining
      print("  ".join(i))
# never found a way to just return the contents but it does print to console
# tried return[''.join i for i in zip(*final_list)]----> failed, gave up, cried



what do the tests say? or can you provide the link to your repl?

it does not work because my function does not return because i could figure out how to return the formatted string only print them.

not that link, whoever uses that link can access and change your code

the link that’s in your url bar

Sorry sir

https://repl.it/@LuisPerez33/boilerplate-arithmetic-formatter-3#arithmetic_arranger.py

I have edited your post adding a space at the beginnning to make the link show

I have deleted your multiplayer link to avoid vandalism to your code

do not call me sir please

issue one: your errors should be exaclty as required, yours have differences

issue two: your function returns nothing when it should return the formatted numbers

make it return something, then see the tests, they will say in what way your output differs so you know what to fix