在一些公共場所經常可以看到一些動態提示的文字,以下代碼即為文字的循環變化代碼:
import sys import time def print_act(word): print('新春佳節快樂'+'\r') #讓光標回到行首 sys.stdout.write("\r") #緩沖區的數據全部輸出 sys.stdout.flush() #遍歷整個單詞 for item in word: #寫到緩沖區 sys.stdout.write(item) #輸出 sys.stdout.flush() #暫停0.3秒 time.sleep(0.3) print('\n'+'2019年02月06日 17:30:36' + '\r') while True: #調用print_act方法 print_act('請勿越過白色安全線!')
結果:
新春佳節快樂 請勿越過白色安全線! 2019年02月06日 17:30:36 新春佳節快樂 請勿越過白色安全線! 2019年02月06日 17:30:36 新春佳節快樂 請勿越過白色安全線! 2019年02月06日 17:30:36 新春佳節快樂 請勿越過白色安全線! 2019年02月06日 17:30:36 ………………(此處會一直循環)
說明:時間可以取當日。
