1

I haven't been able to find out anything on this.
I would like to know how to use a function (e.g. clear_screen) that can print out 20 blank lines.
The last line of my program should be a call to clear_screen.

The start of my code is:

def new_line():
    print
def three_lines():
    new_line()
    new_line()
    new_line()
def nine_lines():
    three_lines()
    three_lines()
    three_lines()
print " "
nine_lines()
print " "

The print function works, but not with clear_screen() and that's what I need working.
If anyone can help me or have any suggestions, that would be great, thanks.

pradyunsg
  • 18,287
  • 11
  • 43
  • 96
user2130691
  • 19
  • 1
  • 5

2 Answers2

3

There isn't a single cross-platform way I think. So instead of relying on os.*, the following could work

print("\n"*20)
Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • Thanks! The start of my code is def new_line(): print def three_lines(): new_line() new_line() new_line() def nine_lines(): three_lines() three_lines() three_lines() print " " nine_lines() print " " and the print function wors, but not with def clear_screen(): and that's what I need to work? – user2130691 Mar 04 '13 at 07:46
  • @user2130691 please add the code to the question. I can't follow without the indents. – Anirudh Ramanathan Mar 04 '13 at 07:54
  • @Cthulhu I posted what seems to be his code in an update... – pradyunsg Mar 04 '13 at 08:18
3

Your clear_screen can be

  1. os.system Based

    def clear_screen():
        import os
        os.system( [ 'clear', 'cls' ][ os.name == 'nt' ] )
    

    Works on unix and Windows.
    Source: Here

  2. Newline-based

    def clear_screen():
        print '\n'*19 # print creates it's own newline
    

As per your comment, it seems your code is

def new_line():
    print
def three_lines():
    new_line()
    new_line()
    new_line()
def nine_lines():
    three_lines()
    three_lines()
    three_lines()
print " "
nine_lines()
print " "

It will work and does,
But Why do you want to have such a long piece of code if print '\n'*8 can do the same?

Speed Test
Even though you don't have a speed restriction, here's some speed stats for 100 runs each

os.system function took 2.49699997902 seconds.
'\n' function took 0.0160000324249 seconds.
Your function took 0.0929999351501 seconds.
Community
  • 1
  • 1
pradyunsg
  • 18,287
  • 11
  • 43
  • 96