python - 发送输入后立即删除输入行
问题描述
我正在寻找一种隐藏在 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
,但是(因为我使用它)并没有做我做的事情想要它,在此先感谢。
解决方案
如果您使用命令提示符来运行您的代码,那么您可以这样做;
from os import system
name = input('Enter your name: ')
system('cls')
print(f'Your name is {name}')
system('cls')
在提交输入后清除控制台,因为它在提供输入后立即运行。
如果您使用的是 mac;
system('clear')
推荐阅读
- python - 如何使用堆叠在 python 中的相同装饰器?
- excel - 从 Now() 函数单元触发更改事件的问题
- java - 即使 redo() 工作正常,为什么 UndoManager.canRedo() 会返回 false?
- javascript - 如何在javascript中动态添加带有id的列表项?
- ebay-api - findItemsAdvanced 突然失败:与服务器的连接异常终止
- javascript - 如何设置 2 个屏幕截图桌面调用之间的时间延迟?
- python-3.x - 包含值的排序列表:无,结果为 TypeError:“NoneType”和“str”的实例之间不支持“<”
- javascript - 我应该如何处理这个nodejs代码来做正确的事情?
- perl - Perl 问题:grep 后删除的内容
- python - 重叠标签:如何在同一个刻度上对名称进行分组?