2015-04-01 83 views
-4

我如何制作一个python密码程序,要求用户输入密码并检查密码是否至少包含8个字符,一个数字,一个大写字母和一个小写字母。如果缺少其中一个,它将打印出一条声明,告诉他们需要添加哪些内容才能确保密码安全。 到目前为止我的代码:使用while循环在python 3.4中创建一个安全的密码程序

incorrectPassword= True 
while incorrectPassword: 
    password = input("type in your password") 
    if len(password < 8): 
     print("your password must be 8 characters long") 
    elif noNum(password == False): 
     print("you need a number in your password") 
    elif noCap(password == False): 
     print("you need a capital letter in your password") 
    elif NoLow(password == False): 
+0

它为我的计算机科学类的任务,我已经厌倦了做变量打印出来的时候在一个密码,但IM的用户类型停留在如何使它检查超过8个字符,如果它有一个大写字母。这是分配 – 2015-04-01 23:29:43

+0

编辑你的问题,包括你已经尝试过的代码以及它失败的方式(错误的结果,错误信息等)。 – TigerhawkT3 2015-04-01 23:38:00

+0

你可以读取该代码吗?我不能,请修复它。 – MrSykkox 2015-04-01 23:46:43

回答

-2

要检查密码字符串包含一个数字,大写字母或类似的,你可以使用正则表达式,这里是一个很好的工具来测试和学习[RegExr(http://www.regexr.com/)。

这是一个很好的做法,有一个函数将检查,验证您的输入,如果你需要做多次e.q.在一个while循环中。

import re 

def checkPassword(password): 
    """ 
    Validate the password 
    """ 
    if len(password) < 8: 
     # Password to short 
     print("Your password must be 8 characters long.") 
     return False 
    elif not re.findall(r'\d+', password): 
     # Missing a number 
     print("You need a number in your password.") 
     return False 
    elif not re.findall(r'[A-Z]+', password): 
     # Missing at least one capital letter 
     print("You need a capital letter in your password.") 
     return False 
    else: 
     # All good 
     print("All good") 
     return True 

# Promt user to enter valid password 
passwordValid = False 
while not passwordValid: 
    password = input("Type in your password: ") 
    passwordValid = checkPassword(password) 
+2

正则表达式对此非常严重的矫枉过正。 – TigerhawkT3 2015-04-01 23:59:06

+0

@ TigerhawkT3你说得对,但由于我们谈论的是8个字符的字符串,这并不是那么严重。例如,你可以降低字符串,并检查两个匹配(检查大写字母)。 – 2015-04-02 00:08:49

+1

对短字符串进行简单检查的事实是严重矫枉过正的原因。 Python有几个基本的功能和方法,对于像这样的问题是理想的。我只希望这个问题不适用于学校作业,因为如果提问者使用这些代码,教授会知道一些事情已经结束。 – TigerhawkT3 2015-04-02 00:12:37

0

len(password - 8)首先尝试比较8到用户输入的密码,这是行不通的。您必须首先找到密码的长度,然后将其与8len(password) - 8进行比较。

noNum(password == False)之类的东西会发送一个布尔值到noNum()函数我推测你有某处,这可能不是它所需要的。您可能需要noNum(password) == True - 因为如果传递的字符串不包含数字,noNum()应返回True

也就是说,Python具有确定字符串是大写,小写还是数字(仅包含数字)的字符串方法。使用这些来评估给定的密码以及any()函数。

incorrectPassword= True 
while incorrectPassword: 
    password = input("type in your password: ") 
    if len(password) < 8: 
     print("your password must be 8 characters long") 
    elif not any(i.isdigit() for i in password): 
     print("you need a number in your password") 
    elif not any(i.isupper() for i in password): 
     print("you need a capital letter in your password") 
    elif not any(i.islower() for i in password): 
     print("you need a lowercase letter in your password") 
    else: 
     incorrectPassword = False 
+0

在你的情况下,任何与检查isupper/islower/isdigit比正则表达式慢。 (time).timeit('any(i.isupper)')运行在1.1296026709896978,其中'timeit.timeit('re.findall(r“[AZ] +”,“asdfqweR”)',setup =“import re” ()为我在“asdfqweR”)')'运行在1.2718868080119137 – 2015-04-02 00:20:06

+0

你熟悉短语“过早优化是万恶之源”吗?此外,我用'perf_counter()'测试了每个版本,而我的测试需要1-5分之一毫秒,而你的测试需要7-8个毫秒,对于几个字符和几百个字符之间的字符串。 – TigerhawkT3 2015-04-02 00:29:16

0
while True : 
     name = input('Enter user name') 
     if name != 'leela': 
       continue 
     password = input('hello,leela Enter password') 
     if password='sana': 
       break 
print('Access granted') 
+0

如果名称包含四位数字并且密码仅包含四位数字 – 2017-09-13 04:56:34

+0

那么我们将执行该程序 – 2017-09-13 04:57:40