2016-11-17 210 views
-1
def IsValidLogin(login_name): 
    lognum = 0 
    login = 0 

    if len(login_name) >= 8: 
    lognum == True 
    elif len(login_name) < 8: 
    lognum == False 


    for chr in login_name: 
    if chr.isalnum() == True: 
     login == True 
    else: 
     login == False 

    if (login == True) and (lognum == True): 
    IsValidLogin == True 
    else: 
    IsValidLogin == False 

    if IsValidLogin == True: 
    print('Login Accepted') 
    else: 
    print('Login Failed') 

    return lognum, login 


login_name = input('Please enter your Login: ') 
lognum, login = IsValidLogin(login_name) 

def IsValidPassword(password): 
    pass_char = ['!','@','#','$','%','^','&','*','(',')','_','+'] 

    pass_len = 0 
    pass_num = 0 
    spec_char = 0 

    for chr in password: 
    if len(password) >= 8: 
     pass_len == True 
    elif len(password) < 8: 
     pass_len == False 

    for chr in password: 
    if chr.isdigit() == True: 
     pass_num == True 
    elif chr.isdigit() == False: 
     pass_num == False 

    for chr in password: 
    if password == pass_char: 
     spec_char == True 
    elif password != pass_char: 
     spec_char == False 

    if (pass_len == True) and (spec_char == True): 
    IsValidPassword == True 
    else: 
    IsValidPassword == False 

    if IsValidPassword == True: 
    print('Password Accepted') 
    else: 
    print('Password Failed') 

    return pass_len, pass_num, spec_char 

password = input('Please enter your password: ') 
pass_len, pass_num, spec_char = IsValidPassword(password) 

登錄名至少需要8個字符並且只能包含字母和數字。驗證登錄名和密碼

密碼長度必須至少爲8個字符,至少包含一個數字和一個特殊字符。

程序運行,但都返回false。

想知道我做錯了什麼。請記住我是Python新手。

回答

0

的線條:

if len(login_name) >= 8: 
    lognum == True 
    elif len(login_name) < 8: 
    lognum == False 

似乎錯了。看起來好像你正在嘗試分配到lognum,而你正在比較lognumTrueFalse=是賦值,==是比較。類似爲:

if chr.isalnum() == True: 
    login == True 
else: 
    login == False 

if (login == True) and (lognum == True): 
    IsValidLogin == True 
    else: 
    IsValidLogin == False 

和功能外其他幾個地方。

只要將相應的==更改爲=它應該工作。

您還可以在密碼部分邏輯問題,即使有這樣的變化:這將始終返回false,因爲它只檢查的最後一個字符,不能同時是數字和特殊字符。

for chr in password: 
    if chr.isdigit() == True: 
     pass_num = True 
    elif chr.isdigit() == False: 
     pass_num = False #On the last pass through, this overwrites the previous value 

    for chr in password: 
    if password == pass_char: 
     spec_char = True 
    elif password != pass_char: 
     spec_char = False #On the last pass through, this overwrites the previous value 

變化elif password != pass_char:elif not spec_char:elif chr.is_digit():elif not pass_num:從被覆蓋保持這一點。