2016-08-20 71 views
1

以下是密碼驗證過程的代碼,至少包含一個小寫字母,一個大寫字母和一個數字。如何在使用正則表達式時連接多個if語句?

import re 
password=raw_input('Enter the Password') 

x= (re.findall(r'[a-z]',password)) 
if len(x)==0: 
    print " at least one 'a-z' requirment not completed" 
else: 
    #(what should i write here to connect this to the next step?) 

y=(re.findall(r'[A-Z',password)) 
if len(y)==0: 
    print "at least one 'A-Z' requirement not completed" 
else: 
    #(what should i write here?) 

z=(re.findall(r'[0-9]',password)) 

if len(z)==0: 
    print "at least one '0-9' requirment not completed" 
else: 
    print ' Good password!' 

我的願望是讓Python的這樣一種方式,如果x不爲0,然後繼續和驗證運行,如果y等於0。如果不是,驗證z不等於0。如果不,告訴用戶密碼是好的。

+0

還有的這個更一般的重複,但在短期:包這一切的功能, 'len(x)= 0'部分的''return'函數早於該函數。那麼你不需要一個'else'子句,並且你只是遍歷每個'if語句'或者返回提前。 (你可以在最後返回'False'或者'True',然後你的函數就是'validate_password'。 – Evert

+0

另一種方法是如果一個'if-elif-elif -...'鏈,並且移動'findall直接分配給該鏈:'elif len(re.findall(r'[A-Z',password))== 0:'等 – Evert

+0

基本上,你的else子句應該是級聯的: – engineer14

回答

0

基本上,你的else子句就是這樣的下一個語句。你幾乎寫了它,你所要做的就是縮進每一張支票。

import re 
    password=raw_input('Enter the Password') 

    x= (re.findall(r'[a-z]',password)) 
    if len(x)==0: 
     print " at least one 'a-z' requirment not completed" 
    else: 
     y=(re.findall(r'[A-Z',password)) 
     if len(y)==0: 
      print "at least one 'A-Z' requirement not completed" 
     else: 
      z=(re.findall(r'[0-9]',password)) 

      if len(z)==0: 
       print "at least one '0-9' requirment not completed" 
      else: 
       print ' Good password!' 

選項2:

import re 
    password=raw_input('Enter the Password') 

    if not (re.findall(r'[a-z]',password)): 
     print " at least one 'a-z' requirment not completed" 
    elif not (re.findall(r'[A-Z',password)): 
     print "at least one 'A-Z' requirement not completed" 
    elif not (re.findall(r'[0-9]',password)): 
     print "at least one '0-9' requirment not completed" 
    else: 
     print ' Good password!' 
1
import re 
password=raw_input('Enter the Password: ') 
problems = [] 

x = re.findall(r'[a-z]',password) 
if not x: 
    problems.append("at least one 'a-z' requirment not completed") 

y = re.findall(r'[A-Z]',password) 
if not y: 
    problems.append("at least one 'A-Z' requirement not completed") 

z = re.findall(r'[0-9]',password) 
if not z: 
    problems.append("at least one '0-9' requirment not completed") 

if problems: 
    print ' There were some problems with your password: ' + ', '.join(problems) 
else: 
    print ' Good password!' 
+0

它不起作用 –

+1

@gurkha_dawg FYI:「它不工作」是極其無益的。它沒有工作,因爲它沒有給出正確的結果,或者因爲它崩潰了? – SethMMorton

+0

@gurkha_dawg我已經修復了'findall(r'[A-Z',password]' - 它應該是'findall(r'[A-Z]',password)',否則會引發意外的正則表達式結束。這個錯誤也在問題中。 – Messa

1

這裏是一個辦法做到這一點只用字符串的方法!

p = raw_input('enter password') 
if p.islower() or p.isupper() or not any(c.isdigit() for c in p): 
    print('bad password') 
+0

關於爲什麼這是一個錯誤的密碼信息呢? – engineer14

2

使用if-elif聲明:

import re 
password=raw_input('Enter the Password') 

if len(re.findall(r'[a-z]',password))==0: 
    print " at least one 'a-z' requirment not completed" 
elif len(re.findall(r'[A-Z]',password)) == 0: 
    print "at least one 'A-Z' requirement not completed" 
elif len(re.findall(r'[0-9]',password)) == 0: 
    print "at least one '0-9' requirment not completed" 
else: 
    print ' Good password!' 

一個演示:

Enter the Password Axa3 
Good password! 

Enter the Password aa3 
at least one 'A-Z' requirement not completed 

Enter the Password A3443E 
at least one 'a-z' requirment not completed