Clear/overwrite Python 中的标准输出
Clear/overwrite standard output in Python
嗨,我正在尝试在 Python 中制作井字游戏,但我 运行 遇到了问题。
正如你在图片上看到的,它在输入后重写播放板,我想要它做的是清除输出然后重写板。因此,它不是一直打印新板,而是只清除当前板并重写它。我搜索了“清晰输出”等,但只找到了这些片段:
import os
clear = lambda: os.system('cls')
或
import os
def clear():
os.system('cls')
但是,它对我不起作用。它只有returns这个符号:
我目前正在 PyCharm 中编写代码,为了清楚起见,我想将其保留在 PyCharm 中。
我发现您的代码存在语法错误,您缺少“:”。
clear = lambda : os.system('cls')
但是避免使用 lambda 并为 clear 定义一个函数,因为它更易于阅读。
def clear():
os.system( 'cls' )
然后您可以使用以下方法清除 window:
clear()
在 jupyter 中添加以下内容对我有用:
from IPython.display import clear_output
clear_output(wait=True)
用于清除 Python 中 Linux 控制台输出的代码:
def clear():
os.system('clear')
首先,使用 Python 包安装程序 pip 安装 IPython:
pip install IPython
在你的脚本中写下以下内容
form IPython.display import clear_output
我想现在应该可以了
这取决于操作系统,
如果你想确定你能做到:
import os, platform
def clear():
if platform.system() == 'Windows':
os.system('cls')
else:
os.system('clear')
并调用函数:
clear()
但是如果你只想要 Windows OS (Windows 10/11/7/8/8.1 ecc..) 就可以了:
import os
clear = lambda:os.system('cls')
你也叫它:
clear()
嗨,我正在尝试在 Python 中制作井字游戏,但我 运行 遇到了问题。
正如你在图片上看到的,它在输入后重写播放板,我想要它做的是清除输出然后重写板。因此,它不是一直打印新板,而是只清除当前板并重写它。我搜索了“清晰输出”等,但只找到了这些片段:
import os
clear = lambda: os.system('cls')
或
import os
def clear():
os.system('cls')
但是,它对我不起作用。它只有returns这个符号:
我目前正在 PyCharm 中编写代码,为了清楚起见,我想将其保留在 PyCharm 中。
我发现您的代码存在语法错误,您缺少“:”。
clear = lambda : os.system('cls')
但是避免使用 lambda 并为 clear 定义一个函数,因为它更易于阅读。
def clear():
os.system( 'cls' )
然后您可以使用以下方法清除 window:
clear()
在 jupyter 中添加以下内容对我有用:
from IPython.display import clear_output
clear_output(wait=True)
用于清除 Python 中 Linux 控制台输出的代码:
def clear():
os.system('clear')
首先,使用 Python 包安装程序 pip 安装 IPython:
pip install IPython
在你的脚本中写下以下内容
form IPython.display import clear_output
我想现在应该可以了
这取决于操作系统, 如果你想确定你能做到:
import os, platform
def clear():
if platform.system() == 'Windows':
os.system('cls')
else:
os.system('clear')
并调用函数:
clear()
但是如果你只想要 Windows OS (Windows 10/11/7/8/8.1 ecc..) 就可以了:
import os
clear = lambda:os.system('cls')
你也叫它:
clear()