验证用户名和密码

时间:2013-11-27 03:23:26

标签: python validation username

我已经问了几个关于这段代码的问题,幸运的是我差不多完成了,但我还有最后一个问题。

这些是我的功能: def valid_username(username):     如果不是username.isalnum():         reason =('用户名只能包含字母数字')         返回'',原因

numupper =0
for c in username:
    if c.isupper():
        numupper = numupper + 1

if numupper <= 0:
    reason=('username must contain at least one uppercase character')
    return '',reason

numlower =0
for c in username:
    if c.islower():
        numlower = numlower + 1

if numlower <= 0:
    reason=('username must contain at least one lowercase character')
    return '', reason

    if len(username)<8:
        reason = ('username must be greater than 8 characters')
        return '',reason

numdigit=0
for c in username:
    if c.isdigit():
        numdigit = numdigit + 1

if numdigit <= 0:
    reason= ('username must contain at least one number')
    return '',reason

else:
    return username, ''

def valid_password(密码):     如果不是password.isalnum():         pwreason =('密码只能包含字母数字')         返回'',pwreason

numupper =0
for c in password:
    if c.isupper():
        numupper = numupper + 1

if numupper <= 0:
    pwreason=('password must contain at least one uppercase character')
    return '',pwreason

numlower =0
for c in password:
    if c.islower():
        numlower = numlower + 1

if numlower <= 0:
    pwreason=('password must contain at least one lowercase character')
    return '', pwreason

    if len(password)<8:
        pwreason = ('password must be greater than 8 characters')
        return '',pwreason

numdigit=0
for c in password:
    if c.isdigit():
        numdigit = numdigit + 1

if numdigit <= 0:
    pwreason= ('password must contain at least one number')
    return '',pwreason

if username in password:
    print('username cannot be used as part of your password')

else:
    return password, ''

用户名一个完美,密码一个也有一定程度。 这是我的计划:

import uservalidation

# get a username from the user
username = input("Username: ")

# validate username
result, reason = uservalidation.valid_username(username)

# if it isn't valid we should tell them why
if not(result):
     print (reason)

# otherwise the username is good - ask them for a password
# get a password from the user
password = input("Password: ")

# determine if the password is valid
pwresult, pwreason = uservalidation.valid_password(password)

# if the password isn't valid we should tell them why
if not(pwresult):
     print (pwreason)
else:
    print ("Username and Password combination is valid!")

唯一似乎不起作用的部分是当我检查密码是否包含用户名时,我收到的错误说明用户名不是全局变量但我认为是因为我问用户在两个函数之外的程序中输入它。谢谢!

2 个答案:

答案 0 :(得分:3)

except用于try...except语句。您尚未在代码中添加try子句。从您的代码中可能看起来您并不真正理解如何正确处理异常。

This将是一个很好的介绍。

答案 1 :(得分:0)

你似乎对try and except clause感到困惑。您可能的目标是这样的,这样可以完全避免使用异常。

def valid_username(username):
    if not username.isalpha():
        reason = 'Username can only contain alpha characters'
        return reason

    if not any(x.isupper() for x in username):
        reason = 'Username must contain at least one uppercase character'
        return reason

    return "Username is good!"

print valid_username('FrankWorks')
print valid_username('frankfails')

或使用您可以执行的例外:

def valid_username_using_exceptions(foo):
    if not foo.isalpha():
        raise ValueError('Username can only contain alpha characters')
    return True

    #.. Other validations functions... etc


failing_username = 'this_will_raise_an_exception_***'
try:
    if valid_username_using_exceptions(failing_username):
        print 'All good'
except ValueError, e:
    print "Failed!", e