如何打印接收输入的前一行? Python
因此,在 Python 中,用户将输入放在控制台的一行上,并且它会保留在那里。我希望代码基本上打印在前一个输入行上,用打印的字符串覆盖最后一行上的用户输入。有什么办法可以做到这一点吗?
太阳系中有多少颗行星?
#用户在这一行输入答案
--->
太阳系中有多少颗行星?
正确的
So in Python, the user places input on a line in the console and it stays there. I want the code to basically print over the previous input line, covering the user input on the last line with a printed string. Is there any way to do that?
How many planets are in the solar system?
#user inputs answer on this line
--->
How many planets are in the solar system?
correct
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个快速修复方法是使用 os.system('cls') 清除控制台并重新打印问题。
代码:
输出:
清除之前:
清除之后:
这效果很好,不需要太多代码。
A quick fix would be to clear the console with os.system('cls') and re-print the question.
Code:
Output:
Before the clear:
After the clear:
This works great and doesn't require much code.