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()
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
For those who are using vs code, then to clear the console output :
import os
os.system('clear')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With