6

わかりましたので、vpython でこの小さなカウントダウン関数を実行しています。現在実行している方法は次のとおりです。

import time
print "5"
time.sleep(1)
print "4"
time.sleep(1)
print "3"
time.sleep(1)
print "2"
time.sleep(1)
print "1"
time.sleep(1)
print "0"
time.sleep(1)
print "blastoff"

もちろん、これは実際には私のコードではありませんが、それをかなりよく示しています。だから私がしたいのは、それを印刷する代わりに 5 4 3 2 1 Blastoff 同じ行に 54321 Blastoff が欲しいということです。少し待って、同じ行に文字を出力するにはどうすればよいでしょうか。私に知らせてください、それは大きな助けになるでしょう

4

3 に答える 3

3

これを試して:

import time

for i in range(5, 0, -1):
    print i, # print in the same line by adding a "," at the end
    time.sleep(1)
    if i == 1:
        print 'Blastoff!'

期待どおりに動作します:

5 4 3 2 1 Blastoff!

編集

...または、スペースなしですべてを印刷する場合(質問には明確に記載されていません):

import time
from __future__ import print_function # not necessary if using Python 3.x

for i in range(5, 0, -1):
    print(i, end="")
    time.sleep(1)
    if i == 1:
        print(' Blastoff!')

上記は印刷されます:

54321 Blastoff!
于 2013-11-27T21:19:57.490 に答える
2

Python 3 ではend=""、print 関数に渡す必要があります。

于 2013-11-28T15:13:22.893 に答える