Carsten Brooks
Carsten Brooks

Reputation: 173

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

Upvotes: 4

Views: 28213

Answers (4)

Swift user
Swift user

Reputation: 26

Try this

Import console console.clear()

Upvotes: 0

15_ Dinesh reddy
15_ Dinesh reddy

Reputation: 59

For those who are using vs code, then to clear the console output :

import os
    os.system('clear')

Upvotes: 3

Sam Chats
Sam Chats

Reputation: 2321

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)

Upvotes: 1

HariKishore
HariKishore

Reputation: 169

From this https://www.jetbrains.com/help/pycharm/interactive-console.html. It basically uses a system based python interpreter, there is no direct way or command to clear Python interpreter console. So you need a system call to clear the Python interpreter console screen. For window system, cls clear the console. For the Linux system, clear command works. It requires the OS library to be imported.

import os clear = lambda: os.system('cls') #on Windows System os.system('clear') #on Linux System clear()

The “lambda” keyword in Python is used to define anonymous functions.

import os is inbuild in python 3

Upvotes: 6

Related Questions