2013-03-04 117 views
1

我一直无法找到任何这方面的信息。
我想知道如何使用可以打印出20个空白行的功能(例如clear_screen)。
我的程序的最后一行应该是致电clear_screen如何在python中打印空行?

我的代码的启动是:

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 " " 

打印功能的工作原理,但不能与clear_screen(),这就是我所需要的工作。
如果任何人可以帮助我或有任何建议,这将是很好,谢谢。

+0

使用诅咒一些速度统计 - Python库 – dawg 2013-03-04 06:33:26

+0

检查HTTP的一部分:// stackoverflow.com/questions/4810537/how-to-clear-the-screen-in-python – avasal 2013-03-04 06:33:38

+0

@drewk:在Windows上不可用。 – 2013-03-04 06:33:55

回答

3

我觉得没有一种跨平台的方式。因此,而不是依靠os.*,下面可以工作

print("\n"*20) 
+0

谢谢!我的代码开始是 高清NEW_LINE(): 打印 高清three_lines(): NEW_LINE() NEW_LINE() NEW_LINE() 高清nine_lines(): three_lines() three_lines() three_lines() print“” nine_lines() print“”和打印函数wors,但不是def clear_screen():这就是我需要的工作? – user2130691 2013-03-04 07:46:54

+0

@ user2130691请将代码添加到问题中。没有缩进我就无法跟上。 – 2013-03-04 07:54:17

+0

@Cthulhu我贴什么似乎是他在更新代码... – pradyunsg 2013-03-04 08:18:50

3

clear_screen可以

  1. os.system基于UNIX和Windows

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

    作品。
    来源:基于换行Here

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

根据您的意见,似乎你的代码是

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 " " 

它会工作,确实,
但是如果print '\n'*8可以做到这一点,为什么你想要这么长的一段代码?

速度测试
即使你没有速度限制,下面是100次每

os.system function took 2.49699997902 seconds. 
'\n' function took 0.0160000324249 seconds. 
Your function took 0.0929999351501 seconds. 
+0

你一定要明白你的第二个“这里”指向对方的回答右边同样的问题? ;):D – 2013-03-04 06:45:30

+0

@Cthulhu是的,其实我打算发布这个,但是你在我之前发布了它,所以我想我会把它链接到你的。 – pradyunsg 2013-03-04 06:48:35