清除/覆盖Python中的标准输出 [英] Clear/overwrite standard output in Python

查看:42
本文介绍了清除/覆盖Python中的标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Python制作井字游戏,但遇到了问题.

Hi I'm trying to make a tic-tac-toe game in Python and I've run into a problem.

您可以在图片上看到,输入后它会重写游戏板,我想要做的是清除输出,然后重写板.因此,它不仅会一直打印新的电路板,而且只会清除当前的电路板并进行重写.我已经搜索了清除输出"等等,但仅找到以下片段:

As you can see on the picture it rewrites the playing board after input, what I want it to do is to clear the output and then rewrite the board. So instead of just printing new boards all the time it only clears the current board and rewrites it. I've searched on "clear output" etc, but found only these snippets:

import os

clear = lambda: os.system('cls')

import os

def clear():
    os.system('cls')

但是,它对我不起作用.它仅返回以下符号:

However, it doesn't work for me. It only returns this symbol:

我目前正在用PyCharm编写我的代码,只是为了清楚起见,我想将其保存在PyCharm中.

I am currently writing my code in PyCharm and just to make it clear, I want to keep it in PyCharm.

推荐答案

我在您的代码中看到语法错误,您缺少:".

I see a syntax error from your code, you are missing the ":".

clear = lambda : os.system('cls')

但是要避免使用lambda并定义一个clear函数,因为它更易于阅读.

However avoid lambda and define a function for clear because it is easier to read.

def clear():
    os.system( 'cls' )

然后您可以使用以下方法清除窗口:

then you can clear the window with:

clear()

这篇关于清除/覆盖Python中的标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆