本文介绍了如何在Python中的raw_input的行上打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,当raw_input要求您键入内容并按Return键时,反馈会打印在新行上.如何在提示行上打印? CR在这种情况下可以工作吗?

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?

演示:

prompt = "Question: "
answer = raw_input(prompt)
print answer
print("Correct!")

键入答案并按Return键后的模拟输出:

Simulated output after typing an answer and pressing Return:

>> Question: my answer
>> Correct!

所需的输出:

>> Correct!

推荐答案

使用 blessings :

from blessings import Terminal
term = Terminal()

raw_input("Question: ")
print(term.move_up() + "Correct!" + term.clear_eol())

很严重,就是这样.

这是一个更奇特的东西:

Here's something fancier:

input(term.red("Question: ") + term.bold)
print(term.normal + term.move_up + term.green("Correct!") + term.clear_eol)

这表明经常调用term.thing是可选的,因为它们的作用类似于属性.这意味着您可以做一些很棒的事情,例如

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))

这篇关于如何在Python中的raw_input的行上打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 23:09