首页 > 解决方案 > 发送输入后立即删除输入行

问题描述

我正在寻找一种隐藏在 WINdows 上为类似聊天的系统输入的输入的好方法

现在,我写了这个

import sys

name = input("Enter your message: ")
sys.stdout.write('\r')
print(message)

但它什么也没做。

我想做这样的事情

Enter your message: Hello

Output:
Hello

那就好多了

Enter your message: Hello

Output:
Enter your message: Hello
Hello

所以最终的输出,在一些消息之后将是

Hello
How are you?
I'm fine thanks.

并不是

Enter your message: Hello
Hello
Enter your message: How are you?
How are you?
Enter your message: I'm fine thanks.
I'm fine thanks. 

在询问之前我已经看到了一些类似我的问题,但我没有发现任何工作,我也无法使用os,因为它必须包含更多输入,我也尝试过使用\r,但是(因为我使用它)并没有做我做的事情想要它,在此先感谢。

标签: pythonpython-3.xinputoutput

解决方案


如果您使用命令提示符来运行您的代码,那么您可以这样做;

from os import system
name = input('Enter your name: ')
system('cls')
print(f'Your name is {name}')

system('cls')在提交输入后清除控制台,因为它在提供输入后立即运行。

如果您使用的是 mac;

system('clear')

推荐阅读