Ask Your Question

Revision history [back]

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

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([])

--------------------------------------------------------------------------------------------------#`

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

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')

**or attach('filename.py')** driver(2,4,5,1) or 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([])

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([])

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

--------------------------------------------------------------------------------------------------

Initiates the sorting algorithm

driver([])

--------------------------------------------------------------------------------------------------#`