I am trying to create python code that checks whether a password is:
我正在尝试创建用于检查密码是否为:
Is at least 6 characters long.
Contains both letters and numbers.
Isn't letters only.
Isn't numbers only.
长度至少为6个字符。包含字母和数字。不仅仅是字母。不仅仅是数字。
Doesn't use any:
Imported libraries.
Loop Structures.
User defined inputs.
不使用任何:导入的库。循环结构。用户定义的输入。
Here is the code:
以下是代码:
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 10
password = input('Enter your Password:\n')
password_length = len(password)
#Do I need the two lines below?
#if (len(password) < 6 or len(password) > 10) :
# print ("Password is Weak - Doesn't Meet Length Requirements")
#below code checks input for alphabetical characters
if password.isalpha :
print ("Password is Weak - Only Contains Letters")
#below code checks input for numerical characters
elif password.isnumeric :
print ("Password is Weak - Only Contains Numbers")
#cannot get it to recognise numbers inputed into password field, mistakes numbers for letters
#below code checks input for both alphabetical and numerical characters
elif password.isalnum :
print ("Password is Strong")
I need help with this (am I missing something?), any help would be appreciated.
我需要帮助(我错过了什么?),任何帮助都将不胜感激。
更多回答
Alphanumeric means that a string contains only letters and numbers, not that a string must contain both letters and numbers. Please update either the title or the question to reflect this distinction.
字母数字表示字符串只包含字母和数字,而不是字符串必须同时包含字母和数字。请更新标题或问题以反映这一区别。
updated title and question, hopefully it clears things up
更新了标题和问题,希望它能澄清问题
优秀答案推荐
import re
def is_valid_alphanumeric_password(password):
if not re.match(r'^[a-zA-Z0-9]{6,10}$', password):
return False
return True
This is a basic code for the check and re or regular expression library is pre-installed it checks for all the condition and returns True or False based on it.
这是一个Check and Re的基本代码,也就是预安装的正则表达式库,它检查所有的条件,并根据它返回True或False。
It actually sounds like your accepted password should have a length of 6 to 10 characters and should contain both numbers and letters:
实际上,您可以接受的密码长度应该为6到10个字符,并且应该同时包含数字和字母:
def is_valid_alphanumeric_password(password):
if not re.search(r'^(?=.*[a-z])(?=.*[0-9])[a-z0-9]{6,10}$', password, flags=re.I):
return False
return True
print(is_valid_alphanumeric_password("abcdef")) # False
print(is_valid_alphanumeric_password("123456")) # False
print(is_valid_alphanumeric_password("abc123")) # True
The regex pattern used here says to match:
这里使用的正则表达式模式表示匹配:
^
from the start of the password
(?=.*[a-z])
assert that at least one letter (any case) occurs
(?=.*[0-9])
assert that at least one digit occurs
[a-z0-9]{6,10}
then match 6 to 10 alphanumeric characters
$
end of the password
Fixed the code
已修复代码
password = input('Enter your Password: ')
if password.isalpha():
print ("Password is Weak - Only Contains Letters")
elif password.isnumeric():
print ("Password is Weak - Only Contains Numbers")
else:
print ("Password is Strong")
Didn't need the following code
我不需要以下代码
MIN_PASSWORD_LENGTH = 6
MAX_PASSWORD_LENGTH = 10
password_length = len(password)
forgot to add () after isnumeric & isalpha
忘记在isnumber&ispha后添加()
更多回答
This answer fails for password='0123456789'
and password='foobar'
. Arguably the title doesn't match the actual question, but it seems clear that a password which meets the length requirement and is only letters or only digits is unacceptable.
当Password=‘0123456789’和Password=‘foobar’时,此答案失败。可以说,标题与实际问题不符,但显然,满足长度要求且仅为字母或数字的密码是不可接受的。
我是一名优秀的程序员,十分优秀!