這裏是我的編程到目前爲止打印文本在同一直線上,並呼籲睡眠功能
import time
from time import sleep
print (print("narwhales");sleep[0.1],print("narwhales");sleep[0.1])
我想要的結果是
narwhales(有點晚)narwhales
不幸的是,它只是給了我一個錯誤。 請保持簡單的解釋,我是python3的新手,我還不知道所有的單詞。
這裏是我的編程到目前爲止打印文本在同一直線上,並呼籲睡眠功能
import time
from time import sleep
print (print("narwhales");sleep[0.1],print("narwhales");sleep[0.1])
我想要的結果是
narwhales(有點晚)narwhales
不幸的是,它只是給了我一個錯誤。 請保持簡單的解釋,我是python3的新手,我還不知道所有的單詞。
在Python 3以下應該工作
from time import sleep
print('narwhales', end='', flush=True); sleep(1); print('narwhales')
首先,腳本打印narwhales
在屏幕上。參數end=''
表示不應該發生換行。參數flush=True
立即將文本刷新到控制檯。該腳本然後等待1秒鐘並打印第二個字符串。這一次跟着一個換行符。
另外請注意,這也可以這樣寫
from time import sleep
print('narwhales', end='', flush=True)
sleep(1)
print('narwhales')
非常感謝你:) –
@JOSHUASTRICKLAND如果這個解決方案回答你的問題,請隨時「接受」這個答案和/或投票。 – PhillipD
什麼是錯誤訊息? – PhillipD
文件「main.py」,第3行 print(print(「narwhales」); sleep [0.1],print(「narwhales」); sleep [0.1]) ^ SyntaxError:invalid syntax exited with non-zero狀態(它指向「;」) –
嵌套的打印語句沒有意義。另外,睡眠時調用睡眠(0.1)而不是睡眠[0.1]。 – PhillipD