1

私はこれについて何も見つけることができませんでした。空行を20行出力できる
関数(例)の使い方を教えてください。 私のプログラムの最後の行は、への呼び出しでなければなりません。clear_screen
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 に答える