2017-10-11 152 views
-3

我已經爲簡單的循環編寫了密碼檢查程序的代碼 - 現在我正在嘗試使用Python類來執行相同的操作。將代碼轉換爲python類

pwd = raw_input('Please enter the passowrd: ') 

flag_upp = 0 
flag_num = 0 
flag_len = 0 

for i in pwd: 
    if(i.isupper()): 
     flag_upp = 1 
    if(i.isdigit()): 
     flag_num = 1 
if(len(pwd)>7): 
    flag_len = 1 

total_flag = flag_upp + flag_num + flag_len 
flags = [flag_upp,flag_num,flag_len] 
notations = ['Upper case missing','Number missing','Length must be minimum 8 chars'] 

if(total_flag==3): 
    print 'Password is valid!' 
else: 
    print 'Invalid password!' 
    for i in range(0,len(flags)): 
     if(flags[i]==0): 
      print notations[i] 
     else: 
      pass 

如果我使用python類來做這個,應該是什麼結構。

+0

我的請求 - 請不要轉換代碼。請先嚐試一下你的結局。把這段代碼放在這裏,尋求幫助。只要將上面的代碼轉換爲類,將無法長期幫助您。 –

+0

面向對象編程在廣闊的概念。谷歌它..瞭解它...試試吧 –

+1

謝謝 - 我確實嘗試並能夠創建下面的一個..它的工作 - – Mady

回答

1
class password: 
    def __init__ (self, pwd): #Self is the automatic object 
     self.pwd = pwd 
     print 'password created' 

    def validate(self): 
     flag_upp = 0 
     flag_num = 0 
     flag_len = 0 

     for i in self.pwd: 
      if(i.isupper()): 
       flag_upp = 1 
      if(i.isdigit()): 
       flag_num = 1 
      if(len(self.pwd)>7): 
       flag_len = 1 

     total_flag = flag_upp + flag_num + flag_len 
     flags = [flag_upp,flag_num,flag_len] 
     notations = ['Upper case missing','Number missing','Length must be minimum 8 chars'] 

     if(total_flag==3): 
      print 'Password is valid!' 
     else: 
      print 'Invalid password!' 
      for i in range(0,len(flags)): 
       if(flags[i]==0): 
        print notations[i] 
      # else: 
      # pass 

D = str(raw_input('Password:')) 
p1 = password(D) 
p1.validate()