Python 创建菜单(覆盖以前的输出)
我想用Python创建一个菜单,显示如下内容:Python 创建菜单(覆盖以前的输出),python,console,overwrite,Python,Console,Overwrite,我想用Python创建一个菜单,显示如下内容: -----Help menu----- Hello and welcome to the help menu! Press Enter to continue. 最困难的部分是我希望以前的输出(从“hello”到“continue”)消失。因此,当我按Enter键时,将显示以下内容: -----Help menu----- The first step is to.... Press Enter to continue. 我曾尝试
-----Help menu-----
Hello and welcome to the help menu!
Press Enter to continue.
最困难的部分是我希望以前的输出(从“hello”到“continue”)消失。因此,当我按Enter键时,将显示以下内容:
-----Help menu-----
The first step is to....
Press Enter to continue.
我曾尝试使用
\r
,但这不允许我使用按Enter键 只需使用清晰的屏幕,您就可以做您想做的事情
如果您在windows上,只需在python代码上执行此操作:
import os
os.system('cls')
如果您使用的是Linux(您可能希望这样做),请执行以下操作:
import os
os.system('clear')
这样,您就可以从屏幕上清除以前的输出并插入新的输出。另一种“清除”终端的方法是简单地放入一组回车符,以将其他所有内容从屏幕上删除
messages = ['step 1', 'step 2', 'step 3']
for m in messages:
print('\n'*50)
print('-----Help menu-----')
print(m)
input('Press Enter to continue')
事实上我在mac电脑上。我可以使用与windows/linux上相同的代码吗?是的,与linux上使用的命令相同,您可以在mac上使用它。