1

我一直无法找到任何关于此的信息。
我想知道如何使用clear_screen可以打印出 20 个空白行的函数(例如)。
我的程序的最后一行应该是调用clear_screen.

我的代码的开头是:

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(),这就是我需要的工作。
如果有人可以帮助我或有任何建议,那就太好了,谢谢。

4

2 回答 2

3

我认为没有单一的跨平台方式。因此,而不是依赖于os.*,以下可以工作

print("\n"*20)
于 2013-03-04T06:33:42.020 回答
3

clear_screen可以

  1. os.system基于

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

    适用于 Unix 和 Windows。
    来源:这里

  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.
于 2013-03-04T06:38:53.570 回答