Arithmetich arranger Error I cant understand

Hello,

The issue here is I the arithmetich arranger I had done returns me an error that doesnt seem to be related to the code itself and I dont know how to get it rigth in order to works without returning this error.

# This entrypoint file to be used in development. Start by reading README.md
from pytest import main



def arithmetic_arranger(problems,resultado = True):
  
    if len(problems) > 4: #Para que no haya más de 4 operaciones matemáticas
        return print("Error: Too many problems.")
    
    

            
    problems_str = str(problems)
    valores = problems_str.split()
    
    valor1 = valores[1] #esto es 32
    valor2 = valores[3] #esto es 698
    valor3 = valores[6] #esto es 3801
    valor4 = valores[8]  #esto es 2
    valor5 = valores[11] # 45
    valor6 = valores [13] # 43
    valor7 = valores [16] #123
    valor8 = valores[18] #49
    signo1 = valores[2] #estos son los signos, siendo este el primero
    signo2 = valores[7]
    signo3 = valores[12]
    signo4 = valores[17]

    
    #Aquí pasamos a transformar los valores que teníamos de strings a enteros para poder operar con ellos, así como a introducir un try and except para el supuesto de que no sean números y no sean trasnformables a int.
    #Además tb ponemos un if para evitar que los valores sean mayores de 4 cifras.
    try:
    
      int_valor1 = int(valor1)
      if int_valor1 > 9999:
          return print("Error: Numbers cannot be more than four digits.")
      int_valor2 = int(valor2)
      if int_valor2 > 9999:
          return print("Error: Numbers cannot be more than four digits.")
          
      int_valor3 = int(valor3)
      if int_valor3 > 9999:
          return print("Error: Numbers cannot be more than four digits.") 
      int_valor4 = int(valor4) 
      if int_valor4 > 9999:
          return print("Error: Numbers cannot be more than four digits.")
      int_valor5 = int(valor5)
      if int_valor5 > 9999:
          return print("Error: Numbers cannot be more than four digits.")
      int_valor6 = int(valor6) 
      if int_valor6 > 9999:
          return print("Error: Numbers cannot be more than four digits.")
      int_valor7 = int(valor7)
      if int_valor7 > 9999:
          return print("Error: Numbers cannot be more than four digits.")
      int_valor8 = int(valor8)
      if int_valor8 > 9999:
          return print("Error: Numbers cannot be more than four digits.")
    except:
        return print("Error: Numbers must only contain digits.")

    #Todo esto sirve para que sólo se puedan poner sumas y restas.
    if signo1 == "+":
     resultado1 = int_valor1 + int_valor2
    elif signo1 == "-":
        resultado1 = int_valor1 - int_valor2
    else:
        return print("Error: Operator must be '+' or '-'.")
    if signo2 == "-":
        resultado2 = int_valor3 - int_valor4
    elif signo2 == "+":
        resultado2 = int_valor3 + int_valor4
    else:
        return print("Error: Operator must be '+' or '-'.")
    if signo3 == "-":
        resultado3 = int_valor5 - int_valor6
    elif signo3 == "+":
        resultado3 = int_valor5 + int_valor6
    else:
        return print("Error: Operator must be '+' or '-'.")
    if signo4 == "+":
       resultado4 = int_valor7 + int_valor8
    elif signo4 == "-":
        resultado4 = int_valor7 - int_valor8
    else:
        return print("Error: Operator must be '+' or '-'.")

    
     #Esta amalgama de prints, simplemente dibuja lo solicitado, es decir las sumas y restas en vertical y tal.
    
    print("    ",valor1, end= ""); print("     ",valor3,end= "") ; print("      ",valor5,end= "") ; print("        ",valor7,end="\n")
    print(" ",signo1, end= ""); print("",valor2, end= ""); print("  ",signo2, end= ""); print("    ", valor4, end= ""); print("    ",signo3, end="");print("",valor6, end= "  "); print("    ",signo4, end= "");print(" ",valor8)
    print("--------", end="    ");print("------", end="    ");print("------", end="    ");print("-------")
    if resultado is True:
        print("   ",resultado1, end="    ");print(" ",resultado2, end="    ");print("  ",resultado3, end="  ");print("      " ,resultado4)  ; 



    
    

    

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



# Run unit tests automatically
main()

The error that it gives to me is:

============================= test session starts ==============================
platform linux -- Python 3.8.12, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/runner/boilerplate-arithmetic-formatter
collected 0 items / 1 error                                                    

==================================== ERRORS ====================================
_______________________ ERROR collecting test_module.py ________________________
ImportError while importing test module '/home/runner/boilerplate-arithmetic-formatter/test_module.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test_module.py:3: in <module>
    from arithmetic_arranger import arithmetic_arranger
E   ImportError: cannot import name 'arithmetic_arranger' from 'arithmetic_arranger' (/home/runner/boilerplate-arithmetic-formatter/arithmetic_arranger.py)
=========================== short test summary info ============================
ERROR test_module.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.33s ===============================

Thank you for reading this to the end, I will be very thankful if you can help me.
Have a good day :grinning:

Test is expecting that arithmetic_arranger function will be in arithmetic_arranger module - arithmetic_arranger.py file.

Ok, I did, and now it returns this:

platform linux -- Python 3.8.12, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/runner/boilerplate-arithmetic-formatter
collected 10 items                                                                          

test_module.py FFFFFFFFFF                                                             [100%]

========================================= FAILURES ==========================================
_______________________ test_template[test_two_problems_arrangement1] _______________________

arguments = [['3801 - 2', '123 + 49']]
expected_output = '  3801      123\n-    2    +  49\n------    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3801 - 2", "123 + 49"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
>       actual = arithmetic_arranger(*arguments)

test_module.py:76: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

problems = ['3801 - 2', '123 + 49'], resultado = True

    def arithmetic_arranger(problems,resultado = True):
    
        if len(problems) > 4: #Para que no haya más de 4 operaciones matemáticas
            return print("Error: Too many problems.")
    
    
    
    
        problems_str = str(problems)
        valores = problems_str.split()
    
        valor1 = valores[1] #esto es 32
        valor2 = valores[3] #esto es 698
>       valor3 = valores[6] #esto es 3801
E       IndexError: list index out of range

arithmetic_arranger.py:14: IndexError
_______________________ test_template[test_two_problems_arrangement2] _______________________

arguments = [['1 + 2', '1 - 9380']]
expected_output = '  1         1\n+ 2    - 9380\n---    ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["1 + 2", "1 - 9380"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
>       actual = arithmetic_arranger(*arguments)

test_module.py:76: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

problems = ['1 + 2', '1 - 9380'], resultado = True

    def arithmetic_arranger(problems,resultado = True):
    
        if len(problems) > 4: #Para que no haya más de 4 operaciones matemáticas
            return print("Error: Too many problems.")
    
    
    
    
        problems_str = str(problems)
        valores = problems_str.split()
    
        valor1 = valores[1] #esto es 32
        valor2 = valores[3] #esto es 698
>       valor3 = valores[6] #esto es 3801
E       IndexError: list index out of range

arithmetic_arranger.py:14: IndexError
_______________________ test_template[test_four_problems_arrangement] _______________________

arguments = [['3 + 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = '    3      3801      45      123\n+ 855    -    2    + 43    +  49\n-----    ------    ----    -----'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["3 + 855", "3801 - 2", "45 + 43", "123 + 49"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
>       actual = arithmetic_arranger(*arguments)

test_module.py:76: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

problems = ['3 + 855', '3801 - 2', '45 + 43', '123 + 49'], resultado = True

    def arithmetic_arranger(problems,resultado = True):
    
        if len(problems) > 4: #Para que no haya más de 4 operaciones matemáticas
            return print("Error: Too many problems.")
    
    
    
    
        problems_str = str(problems)
        valores = problems_str.split()
    
        valor1 = valores[1] #esto es 32
        valor2 = valores[3] #esto es 698
        valor3 = valores[6] #esto es 3801
        valor4 = valores[8]  #esto es 2
        valor5 = valores[11] # 45
>       valor6 = valores [13] # 43
E       IndexError: list index out of range

arithmetic_arranger.py:17: IndexError
_______________________ test_template[test_five_problems_arrangement] _______________________

arguments = [['11 + 4', '3801 - 2999', '1 + 2', '123 + 49', '1 - 9380']]
expected_output = '  11      3801      1      123         1\n+  4    - 2999    + 2    +  49    - 9380\n----    ------    ---    -----    ------'
fail_message = 'Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected different output when calling "arithmetic_arranger()" with ["11 + 4", "3801 - 2999", "1 + 2", "123 + 49", "1 - 9380"]
E       assert None == '  11      3801      1      123         1\n+  4    - 2999    + 2    +  49    - 9380\n----    ------    ---    -----    ------'

test_module.py:77: AssertionError
----------------------------------- Captured stdout call ------------------------------------
Error: Too many problems.
___________________________ test_template[test_too_many_problems] ___________________________

arguments = [['44 + 815', '909 - 2', '45 + 43', '123 + 49', '888 + 40', '653 + 87']]
expected_output = 'Error: Too many problems.'
fail_message = 'Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected calling "arithmetic_arranger()" with more than five problems to return "Error: Too many problems."
E       assert None == 'Error: Too many problems.'

test_module.py:77: AssertionError
----------------------------------- Captured stdout call ------------------------------------
Error: Too many problems.
__________________________ test_template[test_incorrect_operator] ___________________________

arguments = [['3 / 855', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = "Error: Operator must be '+' or '-'."
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that uses the "/" operator to return "Error: Operator must be \'+\' or \'-\'."'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
>       actual = arithmetic_arranger(*arguments)

test_module.py:76: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

problems = ['3 / 855', '3801 - 2', '45 + 43', '123 + 49'], resultado = True

    def arithmetic_arranger(problems,resultado = True):
    
        if len(problems) > 4: #Para que no haya más de 4 operaciones matemáticas
            return print("Error: Too many problems.")
    
    
    
    
        problems_str = str(problems)
        valores = problems_str.split()
    
        valor1 = valores[1] #esto es 32
        valor2 = valores[3] #esto es 698
        valor3 = valores[6] #esto es 3801
        valor4 = valores[8]  #esto es 2
        valor5 = valores[11] # 45
>       valor6 = valores [13] # 43
E       IndexError: list index out of range

arithmetic_arranger.py:17: IndexError
____________________________ test_template[test_too_many_digits] ____________________________

arguments = [['24 + 85215', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = 'Error: Numbers cannot be more than four digits.'
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that has a number over 4 digits long to return "Error: Numbers cannot be more than four digits."'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
>       actual = arithmetic_arranger(*arguments)

test_module.py:76: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

problems = ['24 + 85215', '3801 - 2', '45 + 43', '123 + 49'], resultado = True

    def arithmetic_arranger(problems,resultado = True):
    
        if len(problems) > 4: #Para que no haya más de 4 operaciones matemáticas
            return print("Error: Too many problems.")
    
    
    
    
        problems_str = str(problems)
        valores = problems_str.split()
    
        valor1 = valores[1] #esto es 32
        valor2 = valores[3] #esto es 698
        valor3 = valores[6] #esto es 3801
        valor4 = valores[8]  #esto es 2
        valor5 = valores[11] # 45
>       valor6 = valores [13] # 43
E       IndexError: list index out of range

arithmetic_arranger.py:17: IndexError
______________________________ test_template[test_only_digits] ______________________________

arguments = [['98 + 3g5', '3801 - 2', '45 + 43', '123 + 49']]
expected_output = 'Error: Numbers must only contain digits.'
fail_message = 'Expected calling "arithmetic_arranger()" with a problem that contains a letter character in the number to return "Error: Numbers must only contain digits."'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
>       actual = arithmetic_arranger(*arguments)

test_module.py:76: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

problems = ['98 + 3g5', '3801 - 2', '45 + 43', '123 + 49'], resultado = True

    def arithmetic_arranger(problems,resultado = True):
    
        if len(problems) > 4: #Para que no haya más de 4 operaciones matemáticas
            return print("Error: Too many problems.")
    
    
    
    
        problems_str = str(problems)
        valores = problems_str.split()
    
        valor1 = valores[1] #esto es 32
        valor2 = valores[3] #esto es 698
        valor3 = valores[6] #esto es 3801
        valor4 = valores[8]  #esto es 2
        valor5 = valores[11] # 45
>       valor6 = valores [13] # 43
E       IndexError: list index out of range

arithmetic_arranger.py:17: IndexError
______________________ test_template[test_two_problems_with_solutions] ______________________

arguments = [['3 + 855', '988 + 40'], True]
expected_output = '    3      988\n+ 855    +  40\n-----    -----\n  858     1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with ["3 + 855", "988 + 40"] and a second argument of `True`.'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
>       actual = arithmetic_arranger(*arguments)

test_module.py:76: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

problems = ['3 + 855', '988 + 40'], resultado = True

    def arithmetic_arranger(problems,resultado = True):
    
        if len(problems) > 4: #Para que no haya más de 4 operaciones matemáticas
            return print("Error: Too many problems.")
    
    
    
    
        problems_str = str(problems)
        valores = problems_str.split()
    
        valor1 = valores[1] #esto es 32
        valor2 = valores[3] #esto es 698
>       valor3 = valores[6] #esto es 3801
E       IndexError: list index out of range

arithmetic_arranger.py:14: IndexError
_____________________ test_template[test_five_problems_with_solutions] ______________________

arguments = [['32 - 698', '1 - 3801', '45 + 43', '123 + 49', '988 + 40'], True]
expected_output = '   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028'
fail_message = 'Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.'

    @pytest.mark.parametrize('arguments,expected_output,fail_message', test_cases)
    def test_template(arguments, expected_output, fail_message):
        actual = arithmetic_arranger(*arguments)
>       assert actual == expected_output, fail_message
E       AssertionError: Expected solutions to be correctly displayed in output when calling "arithmetic_arranger()" with five arithmetic problems and a second argument of `True`.
E       assert None == '   32         1      45      123      988\n- 698    - 3801    + 43    +  49    +  40\n-----    ------    ----    -----    -----\n -666     -3800      88      172     1028'

test_module.py:77: AssertionError
----------------------------------- Captured stdout call ------------------------------------
Error: Too many problems.
================================== short test summary info ==================================
FAILED test_module.py::test_template[test_two_problems_arrangement1] - IndexError: list in...
FAILED test_module.py::test_template[test_two_problems_arrangement2] - IndexError: list in...
FAILED test_module.py::test_template[test_four_problems_arrangement] - IndexError: list in...
FAILED test_module.py::test_template[test_five_problems_arrangement] - AssertionError: Exp...
FAILED test_module.py::test_template[test_too_many_problems] - AssertionError: Expected ca...
FAILED test_module.py::test_template[test_incorrect_operator] - IndexError: list index out...
FAILED test_module.py::test_template[test_too_many_digits] - IndexError: list index out of...
FAILED test_module.py::test_template[test_only_digits] - IndexError: list index out of range
FAILED test_module.py::test_template[test_two_problems_with_solutions] - IndexError: list ...
FAILED test_module.py::test_template[test_five_problems_with_solutions] - AssertionError: ...
==================================== 10 failed in 0.31s =====================================


I hope you can help me and so many thanks for answering.

Where are you getting the specific raw indices? The error message is saying they are out of rang efor the list valores

Yeah and I dont get it why it says so because if I try to print the valor 6, it gives me 43, without any problems, I am getting the specific raw indices from “problem” which is a list and the first value of my function.

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

There are the values raw, then I transform them into string (problems_str) and then I split them in order to catch those I want through taking the second in the string (32), the third (698),etc.

problems_str = str(problems)
    valores = problems_str.split()

Then I transform that into integer so I can operate with them to make the final operations results.

When I run it in my virtual enviroment, it doesnt give any problem and give me the correct answers, in replit, I get the correct answers too but also a lot of errors as you can read.

Thank you for answering.

You can’t assume the number of entries in the problems array or the number of digits in each result. You are making your code too tailored to exactly one input.

Yeah…

Now I see it, if one of the problems werent there It would not work.

thank you.

1 Like