0

Here is some example code:

def printAsGrid(listName,rowLength):
    gridStr=''
    for n in range(len(listName)):
        if n % rowLength == 0:
            gridStr += '\n'
            gridStr += listName[n] +''
        else:
            gridStr += listName[n]
        
    print(gridStr)

myList = []
for i in range(20):
    myList.append('a')

print(myList)

for i in range(0,20):
    myList[i] = "b"
    printAsGrid(myList,5)

I've looked here and I know about \r and the ANSI exit codes but it doesn't work for windows terminal (they did work for the ubuntu terminal).

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
1cubealot
  • 5
  • 2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Apr 30 '23 at 14:22
  • `\r` works in Windows Command Prompt (`cmd`). Please show a [mre] of what does not work. – mkrieger1 Apr 30 '23 at 15:35
  • 1
    Does this answer your question? [How can I overwrite/print over the current line in Windows command line?](https://stackoverflow.com/questions/465348/how-can-i-overwrite-print-over-the-current-line-in-windows-command-line) – mkrieger1 Apr 30 '23 at 15:35

1 Answers1

0

If you're okay with clearing the entire terminal after each iteration, you could try using something like

import os

def printAsGrid(listName, rowLength):
    gridStr = ''
    for n in range(len(listName)):
        if n % rowLength == 0:
            gridStr += '\n'
            gridStr += listName[n] + ''
        else:
            gridStr += listName[n]

    # use the appropriate system command to clear your terminal. 
    os.system('clear') 
    # the normal print statement
    print(gridStr)


myList = 20*['a']

for i in range(0, 20):
    myList[i] = "b"
    printAsGrid(myList, 5)

On a side note, you might want to look into list comprehension to make your printAsGrid function a little bit easier to read, such as in this answer.

Hoodlum
  • 950
  • 2
  • 13