Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 退格故障_Python_Python 3.x_Console Application - Fatal编程技术网

Python 退格故障

Python 退格故障,python,python-3.x,console-application,Python,Python 3.x,Console Application,为什么我的Python时钟只从Python2运行,而Python3什么都不做 from __future__ import print_function import time wipe = '\b'*len(time.asctime()) print("The current date and time are: "+' '*len(wipe), end='') while True: print(wipe+time.asctime(), end='') time.sleep(

为什么我的Python时钟只从Python2运行,而Python3什么都不做

from __future__ import print_function
import time
wipe = '\b'*len(time.asctime())
print("The current date and time are: "+' '*len(wipe), end='')
while True:
    print(wipe+time.asctime(), end='')
    time.sleep(1)

在Python3中,需要刷新打印缓冲区以强制将字符写入屏幕

转到脚本的开头,并将循环更改为

while True:
    print(wipe+time.asctime(), end='')
    sys.stdout.flush()
    time.sleep(1)

在Python3中,需要刷新打印缓冲区以强制将字符写入屏幕

转到脚本的开头,并将循环更改为

while True:
    print(wipe+time.asctime(), end='')
    sys.stdout.flush()
    time.sleep(1)

问题不在于python版本,而在于您忘记刷新标准输出。尝试将代码更改为:

from __future__ import print_function
import time
import sys
wipe = '\b'*len(time.asctime())
print("The current date and time are: "+' '*len(wipe), end='')
while True:
    print(wipe+time.asctime(), end='')
    sys.stdout.flush()
    time.sleep(1)

sys.stdout仅在打印换行符时刷新。

问题不在于python版本,而在于您忘记刷新标准输出。尝试将代码更改为:

from __future__ import print_function
import time
import sys
wipe = '\b'*len(time.asctime())
print("The current date and time are: "+' '*len(wipe), end='')
while True:
    print(wipe+time.asctime(), end='')
    sys.stdout.flush()
    time.sleep(1)

sys.stdout仅在打印换行符时才会刷新。

实际上,这对于Python 2.x也是必要的(至少在使用打印函数时是如此)。在我的系统上,在Python 2中没有它也无法工作。在我的系统(Windows 7 x64)上,在Python 2.7中没有它也可以工作。实际上,这对于Python 2.x也是必要的(至少在使用打印功能时是这样)。在我的系统上,Python 2中没有这个功能也不行。在我的系统(Windows 7 x64)上,Python 2.7中没有这个功能也行。