• 注册
当前位置:1313e > python >正文

python中的raw_input怎么用_如何在Python中的raw_input的行上打印?

Usually, when raw_input asks you to type something in and press Return, feedback is printed on a new line. How can I print over the prompt's line? Could a CR work in this case?

Demo:

prompt = "Question: "

answer = raw_input(prompt)

print answer

print("Correct!")

Simulated output after typing an answer and pressing Return:

>> Question: my answer

>> Correct!

Desired output:

>> Correct!

解决方案from blessings import Terminal

term = Terminal()

raw_input("Question: ")

print(term.move_up() + "Correct!" + term.clear_eol())

Seriously, that's it.

Here's something fancier:

input(term.red("Question: ") + term.bold)

print(term.normal + term.move_up + term.green("Correct!") + term.clear_eol)

This shows that often calling term.thing is optional because they act similarly to properties. This means you can do awesome stuff like

from blessings import Terminal

term = Terminal()

question = "{t.red}{}{t.normal}{t.bold}".format

answer = "{t.normal}{t.move_up}{t.green}{}{t.normal}{t.clear_eol}".format

input(question("Question: ", t=term))

print(answer("Correct!", t=term))

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录