热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

Usually,whenraw_inputasksyoutotypesomethinginandpressReturn,feedbackisprintedonanewline.Ho

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



推荐阅读
author-avatar
丶Olrickx
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有