Ask Your Question
0

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('filename.py')    **or attach('filename.py')**

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/sage_server.py", line 1244, in execute
    exec(
  File "", line 1, in <module>
  File "/cocalc/lib/python3.11/site-packages/smc_sagews/sage_salvus.py", line 3823, in attach
    load(fname)
  File "/cocalc/lib/python3.11/site-packages/smc_sagews/sage_salvus.py", line 3919, in load
    exec(
  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/load.py", line 253, in load
    exec(code, globals)
  File "./V4.py", line 217, in <module>
    driver([])

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


        splitter()

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

        # 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])
            copy2.pop(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: 

            print(listC)


        # List is not in numerical order    
        else:

            listA.clear()
            listB.clear()
            listC.clear()

            splitter()

#--------------------------------------------------------------------------------------------------#

#Initiates the sorting algorithm
driver([])

#--------------------------------------------------------------------------------------------------#
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

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

Stats

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

Seen: 133 times

Last updated: Feb 11