7 Replies - 2718 Views - Last Post: 29 September 2012 - 03:39 AM Rate Topic: -----

#1 midknight51  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 47
  • Joined: 25-September 12

Clearing the Screen?

Posted 29 September 2012 - 01:11 AM

Hello everyone,
I've been attempting to teach myself Python for a few months now. I have definantly made some progress and am really enjoying the language! Recently, I have been designing a Tic-Tac-Toe game using ASCII characters. The program works, so now I am doing some cleanup to the code. After searching the internet for a bit I cant seem to find any command that clears the screen.

Here is what happens right now:
Every time a player inputs where they wish to place their 'X' or 'O', I have a command called printBoard that prints the new board onto the screen. This is fine, but obviously, after two turns you have the old boards still on the screen.

My question is, is there a command that I can place into my program that clears the screen BEFORE a new board is printed? That way the interface is not clogged up with old instances of the playing board.
So far this is all I can think of.
def clear():
    for i in range(60):
    print()



Any help would be appreceated! :D

Is This A Good Question/Topic? 0
  • +

Replies To: Clearing the Screen?

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Clearing the Screen?

Posted 29 September 2012 - 01:22 AM

The terminal screen? IDLE? The python interpreter? Also what operating system are you using?
Was This Post Helpful? 0
  • +
  • -

#3 midknight51  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 47
  • Joined: 25-September 12

Re: Clearing the Screen?

Posted 29 September 2012 - 01:48 AM

View PostSimown, on 29 September 2012 - 03:22 AM, said:

The terminal screen? IDLE? The python interpreter? Also what operating system are you using?

Im using Windows, as for what is being cleared, it would be the terminal screen I suppose. When the program is activly in use, I only want one instance of the board on the screen at any given time. Rather than the new one, as well as all of the old ones. Id post a photo of what I mean but my im in the office right now and dont have access to my program.
Here is what my program does right now wile running:

Welcome to Tic-Tac-Toe!
Would you like to be X's or O's?
>>>X
player2 will go first.

   |   |
   |   |
   |   |
---+---+---
   |   | 
   |   |
   |   |
---+---+---
   |   |   
   |   | 
   |   |
Where would you like to move? (1-9)
>>>2
   |   |
   | X |
   |   |
---+---+---
   |   | 
   |   |
   |   |
---+---+---
   |   |   
   |   | 
   |   |
Where would you like to move? (1-9)
etc.
etc.


So as you can see, the first instance of the board (With no X's or O's on it remains on the screen. I would rather just one board be on the screen at any given time for less confusion.
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Clearing the Screen?

Posted 29 September 2012 - 01:57 AM

Hmm, right, I understand. At the point in your program you want to clear the screen can you try:

os.system("cls")


You'll need to import the os module first

This post has been edited by Simown: 29 September 2012 - 02:00 AM

Was This Post Helpful? 0
  • +
  • -

#5 midknight51  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 47
  • Joined: 25-September 12

Re: Clearing the Screen?

Posted 29 September 2012 - 02:06 AM

I'll give it a shot when I get home. Also, im assuming since you asked me what OS I am running on that this would be OS specific? If I tried running this on a Linux OS it would not work?
Was This Post Helpful? 0
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Clearing the Screen?

Posted 29 September 2012 - 02:23 AM

Linux uses os.system("clear") to clear the terminal while Windows is os.system("cls"), you can make it cross platform by checking what OS the application is run on (usually using os.name or platform.system)
Was This Post Helpful? 0
  • +
  • -

#7 midknight51  Icon User is offline

  • New D.I.C Head

Reputation: 20
  • View blog
  • Posts: 47
  • Joined: 25-September 12

Re: Clearing the Screen?

Posted 29 September 2012 - 03:38 AM

Thanks for the help! It appears to do exactly what I want it to do when I am executing the code from the Windows terminal. Just out of curiosity, when executing the code in the Python Interactive Shell it does not seem to use the os command. when executing the code from the interacive shell is there another command I can use to acheive the same thing? Right now im using:

print('\n' * 30)


It works well enough for testing purposes in the shell. If not it's no big deal.
Was This Post Helpful? 0
  • +
  • -

#8 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Clearing the Screen?

Posted 29 September 2012 - 03:39 AM

In the shell I think you are just going to have to use newlines like you suggested.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1