通常,当raw_input要求您键入内容并按Return键时,反馈将打印在新行上.如何打印提示行? CR可以在这种情况下工作吗?
演示:
prompt = "Question: "
answer = raw_input(prompt)
print answer
print("Correct!")
键入答案并按Return键后模拟输出:
>> Question: my answer
>> Correct!
期望的输出:
>> Correct!
解决方法:
使用blessings
:
from blessings import Terminal
term = Terminal()
raw_input("Question: ")
print(term.move_up() + "Correct!" + term.clear_eol())
说真的,就是这样.
这里有更高档的东西:
input(term.red("Question: ") + term.bold)
print(term.normal + term.move_up + term.green("Correct!") + term.clear_eol)
这表明经常调用term.thing是可选的,因为它们的行为类似于属性.这意味着你可以做很棒的事情
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))