我的問題如下:
import time
for i in range(100):
print(f"Total connections: {i} \n Something: {i 1}", end="\r")
time.sleep(0.5)
但我的輸出如下:
Total connections: 1
Total connections: 2
Total connections: 3
Something: 4
我期待這樣的事情:
Total connections: 3
Something: 4
我只想要數字在變化,而且只有兩行。
有人能幫幫我嗎?
uj5u.com熱心網友回復:
讓我們從你擁有的開始:
import time
for i in range(100):
print(f"Total connections: {i} \n Something: {i 1}", end="\r")
time.sleep(0.5)
該time.sleep()方法不會改變輸出,所以讓我們擺脫它:
for i in range(100):
print(f"Total connections: {i} \n Something: {i 1}", end="\r")
讓我們重構它以將范圍的頂部取出到一個變數中:
foobar = 100
for i in range(foobar):
print(f"Total connections: {i} \n Something: {i 1}", end="\r")
您只需要兩行,大概是結尾“總連接數”和“某事”計數。兩者都不需要回圈,所以讓我們擺脫它:
foobar = 100
print(f"Total connections: {foobar} \n Something: {foobar 1}", end="\r")
輸出如下所示:
$ python3 test.py
Total connections: 100
Something: 101
uj5u.com熱心網友回復:
我認為你應該像這樣實作它
import time, os
for i in range(100):
print(f"Total connections: {i} \n Something: {i 1}", end="\r")
os.system('clear')
time.sleep(0.5)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/360470.html
下一篇:如何通過智能手機熱點運行顫振專案
