python-3.xconsolepycharm

Is there a python 3 command to clear the output console?


So I am trying to make a simple Conway's game of life in PyCharm, and I can only get a a bunch outputs and not a video like stream in the output console. Is there a command that will let me clear the output every loop in the program. I have already tried the "sys" commands, and the ANSI escape keys (I hope I spelled that right). Nothing seems to be working! I am using Python 3.

I would like to clear the console on the first print statement in the while loop. If that helps.

import copy
import random
import time
WIDTH = 60
HEIGHT = 10


nextCells = []
for x in range(WIDTH):
    column = []
    for y in range(HEIGHT):
        if random.randint(0, 1) == 0:
            column.append('#')
        else:
            column.append(' ')
    nextCells.append(column)

while True:
    # print('\n\n\n\n')
    currentCells = copy.deepcopy(nextCells)

    for y in range(HEIGHT):
        for x in range(WIDTH):
            print(currentCells[x][y], end='')
        print()

Solution

  • On the command prompt (not PyCharm console), try the colorama library to move the cursor back up and print the next iteration over the current iteration (colorama makes ANSI control codes compatible with Windows):

    (colorama can be installed via pip install colorama)

    import copy
    import random
    import time
    
    import colorama
    colorama.init()
    
    WIDTH = 60
    HEIGHT = 10
    
    nextCells = []
    for x in range(WIDTH):
        column = []
        for y in range(HEIGHT):
            if random.randint(0, 1) == 0:
                column.append('#')
            else:
                column.append(' ')
        nextCells.append(column)
    
    while True:
        #print('\n\n\n\n')
        currentCells = copy.deepcopy(nextCells)
    
        for y in range(HEIGHT):
            for x in range(WIDTH):
                print(currentCells[x][y], end='')
            print()
        for x in range(WIDTH):
            for y in range(HEIGHT):
                leftCoord = (x - 1) % WIDTH
                rightCoord = (x + 1) % WIDTH
                aboveCoord = (y - 1) % HEIGHT
                belowCoord = (y + 1) % HEIGHT
    
                numNeighbors = 0
                if currentCells[leftCoord][aboveCoord] == '#':
                    numNeighbors += 1
                if currentCells[x][aboveCoord] == '#':
                    numNeighbors += 1
                if currentCells[rightCoord][aboveCoord] == '#':
                    numNeighbors += 1
                if currentCells[leftCoord][y] == '#':
                    numNeighbors += 1
                if currentCells[rightCoord][y] == '#':
                    numNeighbors += 1
                if currentCells[leftCoord][belowCoord] == '#':
                    numNeighbors += 1
                if currentCells[x][belowCoord] == '#':
                    numNeighbors += 1
                if currentCells[rightCoord][belowCoord] == '#':
                    numNeighbors += 1
    
                if currentCells[x][y] == '#' and (numNeighbors == 2 or numNeighbors == 3):
                    nextCells[x][y] = '#'
                elif currentCells[x][y] == ' ' and numNeighbors == 3:
                    nextCells[x][y] = '#'
                else:
                    nextCells[x][y] = ' '
    
        # Here we move the cursor back up:
        print(f'\033[{HEIGHT+1}A')
    
        time.sleep(1)