Ask Your Question

How do I insert a list into my python code using Cocalc / Sagemath

asked 2024-02-11 08:16:30 +0100

updated 2024-02-11 17:07:03 +0100

Unfortunately, Input() doesn't work in a .sagews, because every integer in a list is then surrounded by an Integer(Insert list value here), which of course breaks the program.

I want to be able to input a list of integers into the sorting algorithm, so that I could actually utilize Cocalc and SageMath. Any help would be greatly appreciated!

When in the .sagews, I do:

load('')    **or attach('')**

driver(2,4,5,1)        **or driver([2,4,5,1])**

The output:

Error in lines 1-1
Traceback (most recent call last):
  File "/cocalc/lib/python3.11/site-packages/smc_sagews/", line 1244, in execute
  File "", line 1, in <module>
  File "/cocalc/lib/python3.11/site-packages/smc_sagews/", line 3823, in attach
  File "/cocalc/lib/python3.11/site-packages/smc_sagews/", line 3919, in load
  File "<string>", line 1, in <module>
  File "sage/misc/persist.pyx", line 175, in sage.misc.persist.load
    sage.repl.load.load(filename, globals())
  File "/ext/sage/10.2/src/sage/repl/", line 253, in load
    exec(code, globals)
  File "./", line 217, in <module>

Infinite recursion continues, most likely because nothing was inputted into driver([])

I've tried inputting driver([Insert list values here]), driver(Insert list values here), but I just keep getting a wide variety of errors.

My code below works perfectly fine in Visual Studio Code, and sorts any given list inserted into driver([]) towards the very bottom This is only a fragment:

def driver(theInput = []):
    global inputList 
    global copy
    global copy2
    global inputStr
    global original 

    good = 0

    # First iteration, other part of driver is therefore unnecessary. 

    if iterationCount == 0:

        inputList = theInput


        # Copies are necessary, because deleting old lists means deleting parts of the list history, 
        # and future inputList
        copy = listC.copy()
        copy2 = listC.copy()

        # Writes down elements from copy2 to the inputList. 
        # Instead of EX: inputLit = [[1,2,3]], it does: inputList[1,2,3]
        j = 0
        for j in range(len(copy2)):
            inputList.insert(len(inputList), copy2[0])

        # Compares the elements and sees if they are in numerical order. 
        for i in range(len(listC) - 1):

            if listC[i] <= listC[i + 1]:
                good += 1

        # List is in numerical order
        if good == len(listC) - 1: 


        # List is not in numerical order    




#Initiates the sorting algorithm

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2024-02-11 18:04:16 +0100

Alright, I figured it out. In SageMath / Cocalc, when making a python file, you don't have to call the function to start the program. That is alerady done when you type driver(insert value here) in the .sagews file

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2024-02-11 07:20:53 +0100

Seen: 78 times

Last updated: Feb 11