如何打印接收输入的前一行? Python

发布于 2025-01-14 19:46:26 字数 183 浏览 1 评论 0原文

因此,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热风软妹 2025-01-21 19:46:26

一个快速修复方法是使用 os.system('cls') 清除控制台并重新打印问题。

代码:

import os
print('How many planets are in the solar system?')
value = int(input())

os.system('cls')
print('How many planets are in the solar system?')
if value == 8:
    print('Good job you are right!')
else:
    print('You are wrong.')

输出:

清除之前:

How many planets are in the solar system?
3

清除之后:

How many planets are in the solar system?
You are wrong.

这效果很好,不需要太多代码。

A quick fix would be to clear the console with os.system('cls') and re-print the question.

Code:

import os
print('How many planets are in the solar system?')
value = int(input())

os.system('cls')
print('How many planets are in the solar system?')
if value == 8:
    print('Good job you are right!')
else:
    print('You are wrong.')

Output:

Before the clear:

How many planets are in the solar system?
3

After the clear:

How many planets are in the solar system?
You are wrong.

This works great and doesn't require much code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文