7

I'm trying to make a tic-tac-toe game in Python and I've run into a problem.

1

As you can see on the picture it rewrites the playing board after input, what I want it to do is to clear the output and then rewrite the board. So instead of just printing new boards all the time it only clears the current board and rewrites it. I've searched on "clear output" etc, but found only these snippets:

import os

clear = lambda: os.system('cls')

or

import os

def clear():
    os.system('cls')

However, it doesn't work for me. It only returns this symbol:

2

I am currently writing my code in PyCharm and just to make it clear, I want to keep it in PyCharm.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Anton M
  • 81
  • 1
  • 1
  • 5
  • 2
    Did you make sure to actually call the clear function? `clear()` – yelsayed May 06 '16 at 11:30
  • Yea but it only returned this symbol: https://i.gyazo.com/d928370f9a4ebaf1ba3d7e07142d7489.png Sorry I could have mentioned it :P – Anton M May 06 '16 at 11:53
  • Have you tried running your code in a terminal (i.e. outside of Pycharm)? – SiHa May 06 '16 at 12:08
  • No I have not but I kinda wanna use pycharm because I like it as an IDE. @SiHa – Anton M May 06 '16 at 12:17
  • 2
    OK, but that's most likely why your code doesn't work. IDE's are for development (hence the name), in reality your code will be run outside of this environment. In my experience IDE's fall over when you try to do non-standard stuff like this because they are only emulating an CMD window. Also see http://stackoverflow.com/questions/27241268/clear-pycharm-run-window – SiHa May 06 '16 at 12:19
  • bear in mind that the "cls" or "clear" command basically writes n jumplines with n being the height of the command line window. If you'd like to do something clean, you should instead look at flushing the output https://docs.python.org/3.3/library/functions.html#print – zoubida13 May 06 '16 at 14:01
  • Related question: https://stackoverflow.com/q/34388390/245915 – nofinator Nov 12 '21 at 17:14

5 Answers5

18

Adding following in jupyter worked for me:

from IPython.display import clear_output
clear_output(wait=True)
devil in the detail
  • 2,905
  • 17
  • 15
9

I see a syntax error from your code, you are missing the ":".

clear = lambda : os.system('cls')

However avoid lambda and define a function for clear because it is easier to read.

def clear():
    os.system( 'cls' )

then you can clear the window with:

clear()
Barry Scott
  • 799
  • 6
  • 13
  • When I use clear it only returns https://i.gyazo.com/d928370f9a4ebaf1ba3d7e07142d7489.png – Anton M May 06 '16 at 11:53
  • 1
    Where are you running you code? Given you are using CLS that implies you are windows and running python in a CMD.exe window. If you are not try that are see if that solves the problem. – Barry Scott May 06 '16 at 12:08
  • I'm running my code in pycharm that's probably why 'cls' does not work but that's the only way I found when I tried to google it. – Anton M May 06 '16 at 12:14
  • There is no syntax error in the code in the question and all code that is suggested here is already contained in the question. This answer adds nothing. – mkrieger1 Mar 25 '23 at 11:04
2

Code to clear console output in Python for Linux:

def clear():
    os.system('clear')
m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
1

It depends on the Operating System, if you want to be sure you can do:

import os, platform
def clear():
   if platform.system() == 'Windows':
      os.system('cls')
   else:
      os.system('clear')

and call the function:

clear()

but if you want only for Windows OS (Windows 10/11/7/8/8.1 ecc..) is fine this:

import os
clear = lambda:os.system('cls')

and you call it as well:

clear()
Dharman
  • 30,962
  • 25
  • 85
  • 135
XxJames07-
  • 1,833
  • 1
  • 4
  • 17
0

First, install IPython using the Python package installer pip:

pip install IPython

In your script write the following

form IPython.display import clear_output

I guess by now it should be working

MartenCatcher
  • 2,713
  • 8
  • 26
  • 39