Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以向python输出添加颜色?_Python_Colors - Fatal编程技术网

是否可以向python输出添加颜色?

是否可以向python输出添加颜色?,python,colors,Python,Colors,因此,我为我、我的朋友和家人制作了一个小型密码强度测试仪,如下所示: import re strength = ['You didnt type anything','Terrible','weak sause','avarage','Good!','Very Strong', 'THE FORCE IS STRONG WITH THIS ONE'] score = 1 password=(raw_input("please type in the password you would like

因此,我为我、我的朋友和家人制作了一个小型密码强度测试仪,如下所示:

import re
strength = ['You didnt type anything','Terrible','weak sause','avarage','Good!','Very Strong', 'THE FORCE IS STRONG WITH THIS ONE']
score = 1
password=(raw_input("please type in the password you would like rated:"))

if len(password) < 1:
      print strength[0]
if len(password) >=1 and len(password)<=4:
      print strength[1]
else:
    print ""

if len(password) >=7:
    score+=1
    print "password was made stronger by not being short"
else:
    print "Your password is really short, concider making it longer"

if len (password) >=10:
    score+=1
    print "password was made stronger by long"
else:
    print "An even longer password would make it stronger"

if re.search('[a-z]',password) and re.search('[A-Z]', password):
    score+=1
    print "password was made stronger by having upper & lower case letters"
else:
    print "Indlucing both upper and lower case letters will make your password stronger"

if re.search('[0-9]+', password):
    score+=1
    print "Password was made stronger by using numbers"
else:
    print "Using numbers will make your password stronger"

if re.search('[.,!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]',password):
    score+=1
    print "Password was made stronger by using punctuation marks and characters"
else:
    print "Using punctuation marks and characters will make the password stronger"

print "\n final password rating is:"
print strength[score]
重新导入
力量=[‘你什么都没打’、‘可怕’、‘弱酱汁’、‘好’、‘很强’、‘这个力量很强’]
分数=1
password=(原始输入(“请输入您想要的密码:”)
如果len(密码)<1:
打印强度[0]
如果len(密码)>=1且len(密码)=7:
分数+=1
打印“密码因不短而变得更强”
其他:
打印“您的密码很短,请使其更长”
如果len(密码)>=10:
分数+=1
打印“密码被长时间增强”
其他:
打印“更长的密码将使其更强大”
如果重新搜索(“[a-z]”,密码)和重新搜索(“[a-z]”,密码):
分数+=1
打印“通过使用大写和小写字母增强密码”
其他:
打印“清除大写和小写字母将使您的密码更牢固”
如果重新搜索(“[0-9]+”,密码):
分数+=1
打印“通过使用数字增强密码”
其他:
打印“使用数字将增强您的密码”
如果重新搜索(“[,!,@,#,$,%,^,&,*,?,,~,-,(,)]”,密码):
分数+=1
打印“使用标点符号和字符增强密码”
其他:
打印“使用标点符号和字符将增强密码”
打印“\n最终密码等级为:”
打印强度[分数]
我希望做的是:

第一-在我给用户的关于密码内容的评论中添加颜色,好的评论,如:
“密码使用数字增强”
将有绿色输出,而建设性反馈,如
“使用数字增强密码”
将有红色输出,使用户更容易发现其密码的优点和缺点

第二,我想知道,如果它的工作原理相同,我可以在我的上述“强度”列表中的某些项目的颜色?把前两个变成红色,中间一对变成黄色,最后一对变成绿色

如果你的控制台(像你的标准ubuntu控制台)理解,你可以使用它们

这里有一个例子:

print ('This is \x1b[31mred\x1b[0m.')
您还可以使用“clrprint”模块,该模块也适用于空闲、终端和PowerShell

pip安装CLR打印


由于对python非常陌生,我错过了这里给出的一些非常简单和有用的命令:-


最终决定使用CLINT作为答案,这是伟大而聪明的人给出的答案

IDLE的控制台不支持ANSI转义序列,也不支持任何其他形式的转义来为输出着色

您可以学习如何直接与IDLE的控制台通信,而不是像对待普通stdout一样对待它并打印到它(这就是它如何对语法进行颜色编码),但这相当复杂。文档只是告诉您使用IDLE本身的基本知识,它的
idlelib
库没有文档(好的,只有一行文档-“(2.3中新增)IDLE开发环境的支持库。”-如果您知道在哪里可以找到它,但这不是很有帮助)。所以,你需要要么,要么做大量的尝试和错误,甚至开始


或者,您可以从命令行而不是从空闲运行脚本,在这种情况下,您可以使用终端处理的任何转义序列。大多数现代终端将至少处理基本的16/8色ANSI。许多将处理16/16或扩展的xterm-256颜色序列,甚至是完整的24位颜色。(我相信,
gnome终端
是Ubuntu的默认配置,在其默认配置中,它将处理xterm-256,但这确实是超级用户或AskUbuntu的问题。)

学习阅读
termcap
条目以了解要输入哪些代码是复杂的…但是如果您只关心一个控制台,或者愿意假设“几乎所有的东西都能处理基本的16/8色ANSI,任何不需要的东西,我都不关心”,那么您可以忽略该部分,只需根据它们硬编码,例如

一旦知道要发出什么,只需在打印之前将代码放入字符串中即可


但是有一些库可以让这一切变得更容易。Python内置的一个非常好的库是。这可以让你接管终端,做一个全屏幕的GUI,有颜色、旋转光标和任何你想要的东西。当然,对于简单的用途来说,它有点重。像往常一样,通过搜索PyPI可以找到其他库。

嘿,伙计-我真的去问了那个问题,但因为它看起来像是空白页上的颜色设置,我真的不知道如何将它合并到已经生成的代码中的特定行中。拼写错误…如果你介意的话。。。。酱汁=酱汁,avarage=一般,concider=顾问我很在乎,谢谢!这只是我在几分钟内做的一件很快的事情-还没有拼写检查。我在ubuntu 12.04 atm中使用IDLE。。。如果它确实支持它,你介意根据我的代码给我一个小例子吗?哪怕是一条线?IDLE不支持ANSI颜色代码。@Giladild我添加了一个例子。
from clrprint import *
clrhelp() # print's available colors and usage

user_input = clrinput("input please: ",clr='r') # just like input() [color is red]
clrprint('your text',user_input,clr='green') # just like print()