Scientific Computing with Python Projects - Arithmetic Formatter

Tell us what’s happening:
Describe your issue in detail here.
Hello guys, i’m having problem with output of fuction, i mean, when i use “print” to see my aswer, all is rigth, however when my output is in return line, ‘\n’ becomes just a string ‘\n’ and not create a new line
Could you help me please?

Your code so far


    t1 = len(str(valor_1))
    t2 = len(str(valor_2))
    t3 = len(str(soma))

    #linha 1
    esp = ' '*(traços - t1)
    #linha 2
    esp2 = ' '*(traços -1 - t2) #um espaço é do operador
    #linha 3
    d = '-' *traços
    #linha 4
    esp3 = ' '*(traços - t3)

    #Escrevendo a string
    l1 = f'{esp}{valor_1}'
    l2 = f'{op}{esp2}{valor_2}'
    l3 = f'{d}'
    l4 = f'{esp3}{soma}'
    au = '\n'

  #saida
  if args:
    saida = l1 + '\n' + l2 + '\n' + l3+ '\n'+ l4
    #saida = f'{esp}{valor_1}\n{op}{esp2}{valor_2}\n{d}\n{esp3}{soma}'
  else:
    saida = l1 + '\n' + l2 + '\n' + l3
    #saida = f'{esp}{valor_1}\n{op}{esp2}{valor_2}\n{d}'
  
  print(saida, type(saida))
  return saida

Your browser information:

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

boilerplate-arithmetic-formatter - Python Repl - Replit)

Hello!
Your function has a problem but it’s not the one you describe.
You can’t see a string that is not printed, and in the string the \n is the information for the print function to start a new line when… doing the printing stuff :sweat_smile:

Your function works as intented if there is only one calculation, so look for why the other ones are missing.

a little hint where to look: l1 = f'{esp}{valor_1}'

Hello!
Thanks a lot for your aswer!
However i couldn’t find my mistake… could you be more specific, i did some modifications but still don’t work

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

  #print(args)
  #arranged_problems = []

  for index, value in enumerate(problems):
    # dividir
    #Ex: ["22","+","12"]
    Opdividir = value.split(" ")

    if not( Opdividir[1] == '+' or Opdividir[1] == '-'):
    #sabemos que o python começa a contar do zero, e o operador é o item do meio, logo o item 1
      return "Error: Operator must be \'+\' or \'-\'."
    
    #Verificação dos valores
    #verficar se pode ser feita a conversão para números(int)
    try:
      valor_1 = int(Opdividir[0])
      valor_2 = int(Opdividir[2])
    except ValueError:
      return 'Error: Numbers must only contain digits.'

    #verificando se os números possuem no máximo 4 dígitos
    if valor_1 > 9999:
      return 'Error: Numbers cannot be more than four digits.'
    if valor_2 > 9999:
      return 'Error: Numbers cannot be more than four digits.'
    
    #Soma ou subtração
    op = Opdividir[1]
    if op == '+':
      soma = valor_1+valor_2
    else:
      soma =  valor_1-valor_2

    #verificando o maior valor
    maior = max(valor_1,valor_2)
    #quantidade de traços que devem haver na operação
    traços = len(str(maior))
    traços = traços +2
#############
# Até aqui tudo ok
#

    t1 = len(str(valor_1))
    t2 = len(str(valor_2))
    t3 = len(str(soma))

    #linha 1
    esp = ' '*(traços - t1)
    #linha 2
    esp2 = ' '*(traços -1 - t2) #um espaço é do operador
    #linha 3
    d = '-' *traços
    #linha 4
    esp3 = ' '*(traços - t3)

    #Escrevendo a string
    l1 = esp+str(valor_1)
    l2 = f"{op}{esp2}{valor_2}"
    l3 = f"{d}"
    l4 = f"{esp3}{soma}"
    au = '\n'

  #saida
  if args:
    saida = l1 + '\n' + l2 + '\n' + l3+ '\n'+ l4
    #saida = f'{esp}{valor_1}\n{op}{esp2}{valor_2}\n{d}\n{esp3}{soma}'
  else:
    saida = l1 + '\n' + l2 + '\n' + l3
    #saida = f'{esp}{valor_1}\n{op}{esp2}{valor_2}\n{d}'
  
  print(saida, type(saida))
  return saida

i forked your repl.it and I ran it with the tester function turned off and I got this output:

 python main.py
  123
+  49
----- <class 'str'>
  123
+  49
-----

So I’m confused why you think this output is correct?

here is the forked repl.it (I didn’t change anything except comment out the testing line in main.py)
forked-repl.it

What does the loop do in each iteration with l1?

First iteration:
Calculate esp, calculate valor_1, assign it to l1
Second iteration:
Calculate esp, calculate valor_1, assign it to l1

Now here is the problem…whatever was calculated in the first iteration and was saved in l1 got deleted, because instead of making l1 a longer string by appending the result of the second iteration, l1 gets a completely new value.

print(arithmetic_arranger(["32 + 698", "3801 - 2", "45 + 43", "123 + 49"])) was the example in the main.py, so you probably forgot somewhere along the way that your output should contain 4 arranged problems instead of 1.

You can add a lot of your own function calls to the main.py to test your function with output by yourself, it won’t affect the prepared tests. Maybe that could help you to find errors faster (+ output of the tests).

Thanks a lot Tzerio and hbar1st!
I understand what you explained to me and i think that i solve the problem about iteration
Now i have to solve the problem about my output, i really appreciate your help

def arithmetic_arranger(problems, args = False):
  saida2 = []
  if len(problems)> 5:
    return('Error: Too many problems.')

  #print(args)
  #arranged_problems = []

  for index, value in enumerate(problems):
    # dividir
    #Ex: ["22","+","12"]
    Opdividir = value.split(" ")
    #print(Opdividir)
    #print(len(Opdividir))
    #print("OPDIVIDIR [1]")
    #print("\n")
    #print(Opdividir[1])

          

    if not( Opdividir[1] == '+' or Opdividir[1] == '-'):
    #sabemos que o python começa a contar do zero, e o operador é o item do meio, logo o item 1
      return "Error: Operator must be \'+\' or \'-\'."
    
    #Verificação dos valores
    #verficar se pode ser feita a conversão para números(int)
    try:
      valor_1 = int(Opdividir[0])
      valor_2 = int(Opdividir[2])
    except ValueError:
      return 'Error: Numbers must only contain digits.'

    #verificando se os números possuem no máximo 4 dígitos
    if valor_1 > 9999:
      return 'Error: Numbers cannot be more than four digits.'
    if valor_2 > 9999:
      return 'Error: Numbers cannot be more than four digits.'
    
    #Soma ou subtração
    op = Opdividir[1]
    #print(op)
    #print(valor_1)
    if op == '+':
      soma = valor_1+valor_2
    else:
      soma =  valor_1-valor_2
    
    if valor_1<10000:


      #for i in len(Opdividir):

      #verificando o maior valor
      maior = max(valor_1,valor_2)
      #quantidade de traços que devem haver na operação
      traços = len(str(maior))
      traços = traços +2
  #############
  # Até aqui tudo ok
  #

      t1 = len(str(valor_1))
      t2 = len(str(valor_2))
      t3 = len(str(soma))

      #linha 1
      esp = ' '*(traços - t1)
      #linha 2
      esp2 = ' '*(traços -1 - t2) #um espaço é do operador
      #linha 3
      d = '-' *traços
      #linha 4
      esp3 = ' '*(traços - t3)

      #Escrevendo a string
      l1 = esp+str(valor_1)
      l2 = op+esp2+str(valor_2)
      l3 = d
      l4 = esp3+str(soma)
      au = '\n'

      #saida
      if args:
        saida = l1 + '\n' + l2 + '\n' + l3+ '\n'+ l4
        #saida = l1 +l2+l3+l4
        #saida = f"{esp}{valor_1}\n{op}{esp2}{valor_2}\n{d}\n{esp3}{soma}"
      else:
        saida = l1 + '\n' + l2 + '\n' + l3
        #saida = saida = l1 +l2+l3
        #saida = f"{esp}{valor_1}\n{op}{esp2}{valor_2}\n{d}"
      saida2.append(saida)
      print(saida)
  print('\n')
  #print(saida2)
  return saida2